SQLite数据库的简单使用
1.创建继承SQLiteOpenHelper类的辅助类 import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBaseHelper extends SQLiteOpenHelper {
//表名和字段名
public static final String TABLE_NAME_NOTES = "notes";
public static final String COLUMN_NAME_NOTE_NAME = "name";
public static final String COLUMN_NAME_NOTE_CONTENT = "content";
public static final String COLUMN_NAME_NOTE_DATE = "date";
//第二个参数为数据库名
public DataBaseHelper(Context context) {
super(context,"db_note",null,1);
}
/** * 当第一次打开数据库,表不存在时调用,以创建表 */
@Override
public void onCreate(SQLiteDatabase db) {
// 1.笔记的名字 2.内容 2.完成的日期
db.execSQL("CREATE TABLE " + TABLE_NAME_NOTES + "(" + COLUMN_NAME_NOTE_DATE
+ " Text PRIMARY KEY," + COLUMN_NAME_NOTE_NAME
+ " Text NOT NULL DEFAULT ""," + COLUMN_NAME_NOTE_CONTENT
+ " Text NOT NULL DEFAULT """ + ")");
}
/** * 当表存在且是旧版本时调用,删除旧表,创建新表 用户更新 */
@Override
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion) {
// TODO Auto-generated method stub
}
}
2.关于SQLite增删改查的简单操作 (1) 增 //创建相关对象
DataBaseHelper database;
SQLiteDatabase db;
database=new DataBaseHelper(Context context);
//注意Readable和Writable的区别
db=database.getReadableDatabase();
String title=etTitle.getText().toString();
String content=etContent.getText().toString();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
String date=format.format(new Date());
if(!title.isEmpty() && !content.isEmpty())
{
ContentValues cv = new ContentValues();//实例化一个 ContentValues用来装载待插入的数据
cv.put("username","Jack Johnson");//添加用户名
cv.put("name",title); //添加密码
cv.put("content",content);
cv.put("date",date);
db.insert("notes",null,cv);//执行插入操作
}
(2) 删除操作 //创建相关对象
DataBaseHelper database;
SQLiteDatabase db;
database=new DataBaseHelper(Context context);
//注意Readable和Writable的区别
db=database.getReadableDatabase();
String whereClause = "name=? and date=?";//删除的条件
String whereArgs[] ={title,date};//删除的条件参数 db.delete("notes",whereClause,whereArgs);//执行删除
(3)修改操作 //创建相关对象
DataBaseHelper database;
SQLiteDatabase db;
database=new DataBaseHelper(Context context);
//注意Readable和Writable的区别
db=database.getReadableDatabase();
ContentValues cv = new ContentValues();//实例化 cv.put("name",etTitle.getText().toString());//添加要更改的字段及内容
cv.put("content",etContent.getText().toString());
String whereClause = "date=?";//修改条件
String[] whereArgs = {etDate.getText().toString()};//修改条件的参数
db.update("notes",cv,whereArgs);//执行修改
(4) 查找操作 public Cursor query(String table,String[] columns,String selection,String[] selectionArgs,String groupBy,String having,String orderBy,String limit); 各个参数的意义说明: 参数table:表名称 参数columns:列名称数组 参数selection:条件字句,相当于where 参数selectionArgs:条件字句,参数数组 参数groupBy:分组列 参数having:分组条件 参数orderBy:排序列 参数limit:分页查询限制 参数Cursor:返回值,相当于结果集ResultSet Cursor是一个游标接口,提供了遍历查询结果的方法,如移动指针方法move(),获得列值方法getString()等. 这边以有ID、用户名、密码这3个字段的名为”usertable”的表查询为例: private void query(SQLiteDatabase db)
{
//查询获得游标
Cursor cursor = db.query ("usertable",null);
//判断游标是否为空
if(cursor.moveToFirst() {
//遍历游标
for(int i=0;i<cursor.getCount();i++){
cursor.move(i);
//获得ID
int id = cursor.getInt(0);
//获得用户名
String username=cursor.getString(1);
//获得密码
String password=cursor.getString(2);
//输出用户信息 System.out.println(id+":"+sname+":"+snumber);
}
}
} (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |