package com.jiao.sqlite;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class DbDemo extends Activity {
/**
* sqlite Demo
*
* 供Activity私有访问的数据库 没有使用ContentProvider 方式 增加 查询数据
*
* @author daguangspecial@gmail.com
*
*/
EditText inputTxt;
Button btnAdd;
Button btnViewAll;
TextView viewAll;
DBHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
// 初始化UI
btnAdd = (Button) findViewById(R.id.btnAdd);
btnViewAll = (Button) findViewById(R.id.btnViewAll);
viewAll = (TextView) findViewById(R.id.viewAll);
inputTxt = (EditText) findViewById(R.id.txtInput);
// 初始化DB
db = new DBHelper(this);
// 初始化监听
OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
if (v.getId() == R.id.btnAdd) {
// 增加
boolean flat=db.save(inputTxt.getText().toString());
if(flat){
Toast.makeText(DbDemo.this,"添加成功!",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(DbDemo.this,"添加失败!",Toast.LENGTH_LONG).show();
}
} else if (v.getId() == R.id.btnViewAll) {
// 浏览所有数据
Cursor cur = db.loadAll();
StringBuffer sf = new StringBuffer();
cur.moveToFirst();
while (!cur.isAfterLast()) {
sf.append(cur.getInt(0)).append(" : ").append(
cur.getString(1)).append("/n");
cur.moveToNext();
}
cur.moveToFirst();
viewAll.setText(sf.toString());
}
}
};
btnAdd.setOnClickListener(listener);
btnViewAll.setOnClickListener(listener);
}
@Override
public boolean onKeyDown(int keyCode,KeyEvent event) {
// TODO Auto-generated method stub
switch(keyCode){
case KeyEvent.KEYCODE_BACK:
db.close();
this.finish();
return true;
}
return super.onKeyDown(keyCode,event);
}
}
DBHelper类:
package com.jiao.sqlite;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.sqliteDatabase;
import android.util.Log;
/**
* 数据库操作工具类
*
* @author daguangspecial@gmail.com
*
*/
public class DBHelper {
private static final String TAG = "DBDemo_DBHelper";// 调试标签
private static final String DATABASE_NAME = "dbdemo.db";// 数据库名
sqliteDatabase db;
Context context;//应用环境上下文 Activity 是其子类
DBHelper(Context _context) {
context = _context;
//开启数据库
db = context.openOrCreateDatabase(DATABASE_NAME,Context.MODE_PRIVATE,null);
CreateTable();
Log.v(TAG,"db path=" + db.getPath());
}
/**
* 建表
* 列名 区分大小写?
* 都有什么数据类型?
* sqlite 3
* TEXT 文本
NUMERIC 数值
INTEGER 整型
REAL 小数
NONE 无类型
* 查询可否发送select ?
*/
public void CreateTable() {
try {
db.execsql("CREATE TABLE t_user (" +
"_ID INTEGER PRIMARY KEY autoincrement,"
+ "NAME TEXT"
+ ");");
Log.v(TAG,"Create Table t_user ok");
} catch (Exception e) {
Log.v(TAG,"Create Table t_user err,table exists.");
}
}
/**
* 增加数据
* @param id
* @param uname
* @return
*/
public boolean save(String uname){
String sql="";
try{
sql="insert into t_user(NAME) values('"+uname+"')";
db.execsql(sql);
Log.v(TAG,"insert Table t_user ok");
return true;
}catch(Exception e){
Log.v(TAG,"insert Table t_user err,sql: "+sql);
return false;
}
}
/**
* 查询所有记录
*
* @return Cursor 指向结果记录的指针,类似于JDBC 的 ResultSet
*/
public Cursor loadAll(){
Cursor cur=db.query("t_user",new String[]{"_ID","NAME"},null,null);
return cur;
}
public void close(){
db.close();
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:id="@+id/txtInput"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增加"
/>
<Button
android:id="@+id/btnViewAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查询所有"
/>
<TextView
android:id="@+id/viewAll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
结果如图所示:
原文链接:https://www.f2er.com/sqlite/202949.html