//20140424
创建数据库帮助类DataBaseHelper,继承SQLiteOpenHelper,
1. 编写构造函数,实现数据库创建;
public static final String TAG = "ListViewActivity";
private static int VERSION = 1;
private static final String TABLE_NAME = "user1";
private static final String SQL_CREATE = "CREATE TABLE user1(_id integer not null primary key autoincrement,"
+ "name text not null,"
+ "number text not null)";/* 表中主键id 要写成 _id
* 数据库版本 >=1
*/
public DataBaseHelper(Context context, String name){
this(context,TABLE_NAME,null,VERSION); // context是Activity
}
public DataBaseHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
2. 复写onCreate()方法,此方法只会在数据库不存在的时候调用,也就是说,数据库存在的情况下则不会调用。
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
Log.i(TAG, "onCreate ");
db.execSQL(SQL_CREATE);
}
3. insert
public void insertDataBase(SQLiteDatabase db,String name,String number)throws SQLiteException{
ContentValues values = new ContentValues();
Log.i(TAG,"insertDataBase : name = "+name+"number = "+number);
values.put("name", name);
values.put("number",number);
db.insert(TABLE_NAME, null, values);
}
4. updata
5. delete
public void updataDataBase(SQLiteDatabase db,int id,ContentValues values){
String[] args = {String.valueOf(id)}; // args不能写成 new String[]{"id"} 下同
db.update(TABLE_NAME, values,"_id=?",args);
}
public void deleteUserDataToDataBase(SQLiteDatabase db,int id){
Log.i(TAG, "id = "+id);
String[] args = {String.valueOf(id)};
db.delete(TABLE_NAME, "_id=?", args);
}
6. select
public Cursor getCursor(SQLiteDatabase db){
return db.query(TABLE_NAME, new String[]{"_id","name","number"}, null, null, null, null, null);
}
7. version updata,在此没有使用。
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}// 不废话了,上代码:
package com.example.listviewdatabase;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DataBaseHelper extends SQLiteOpenHelper {
public static final String TAG = "ListViewActivity";
private static int VERSION = 1;
private static final String TABLE_NAME = "user1";
private static final String SQL_CREATE = "CREATE TABLE user1(_id integer not null primary key autoincrement,"
+ "name text not null,"
+ "number text not null)";
public DataBaseHelper(Context context, String name){
this(context,TABLE_NAME,null,VERSION);
}
public DataBaseHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
Log.i(TAG, "onCreate ");
db.execSQL(SQL_CREATE);
}
public void insertDataBase(SQLiteDatabase db,String name,String number)throws SQLiteException{
ContentValues values = new ContentValues();
Log.i(TAG,"insertDataBase : name = "+name+"number = "+number);
values.put("name", name);
values.put("number",number);
db.insert(TABLE_NAME, null, values);
}
public void updataDataBase(SQLiteDatabase db,int id,ContentValues values){
String[] args = {String.valueOf(id)};
db.update(TABLE_NAME, values,"_id=?",args);
}
public void deleteUserDataToDataBase(SQLiteDatabase db,int id){
Log.i(TAG, "id = "+id);
String[] args = {String.valueOf(id)};
db.delete(TABLE_NAME, "_id=?", args);
}
public Cursor getCursor(SQLiteDatabase db){
return db.query(TABLE_NAME, new String[]{"_id","name","number"}, null, null, null, null, null);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
此文章是本人学习笔记,能力有限,相互学习,欢迎点评,谢谢...
标签:sqlite,String,demo,db,public,Android,null,id,name From: https://blog.51cto.com/u_16068487/6186214