SQLite数据库以及增删改查的案例
Android使用开源的与操作系统无关的SQL数据库——SQLite
一:在命令行下创建数据库:
1.启动模拟器后,打开命令行,执行adb shell
2.进入所在工程目录
3.执行sqlite3 mydb创建数据库文件
:表示结尾,--表示注解
二:包Android.database.sqlite包含了使用SQLite数据库的所有API
SQL数据库主要概念之一就是Schema——一个关于如何组织数据库的定义
单表定义表明和列表:
public final class FeedReaderContract{
public FeedReaderContract(){
/*Inner class that defines the table contents*/
public static abstract class FeedEntry implements BaseColumns{
public static final String TABLE_NAME="entry";
public static final String COLUMN_NAME_ENTRY_ID="entryid";
public static final String COLUMN_NAME_TITLE="title";
public static final String COLUMN_NAME_SUBTITLE="subtitle";
}
}
使用SQLiteOpenHelper创建数据库
代码案例:
//从SQLiteOpenHelper派生一个类
public class FeedReaderDbHelper extents SQLiteOpenHelper{
public static final int DATABASE_VERSION=1;
public static final String DATABASE_NAME="FeedReader.db";
//构造函数产生一个库
public FeedReaderDbHelper(Context context){super(context,DATABASE_NAME,null,DATABASE_VERSION);}
//重点,库产生之后形成一个表
public void onCreate(SQLiteDatabase db){db.execSQL(SQL_CREATE_ENTRIES);}
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
db.execSQL(SQL_DELETE_ENTRIES);onCreate(db);
}
public void onDowngrade(SQLiteDatabase db,int oldVersion,int newVersion){
onUpgrade(db,oldVersion,newVersion);
}
}
//使用以下语句创建数据库助手类对象
FeedReaderDbHelper mDbHelper =new FeedReaderDbHelper(getContext());
插入数据:
代码案例:
//在私有目录产生db,文件和表,然后插入数据
//插入两种方法
SQLiteDatabase db=new mDbHelper.getWritableDatabase();
//Create a new map of values,where column names are the keys
ContentValues values=new ContentValues();
values.put{FeedEntry.COLUMN_NAME_ENTRY_ID,id);
values.put{FeedEntry.COLUMN_NAME_TITLE,title);
values.put{FeedEntry.COLUMN_NAME_CONTENT,content);
//Insert the new row,returning the primary key values of the new row
long new Rowld;
newRowld=db.inset{
FeedEntry.TABLE_NAME,
FeedEntry.COLUMN_NAME_NULLABLE,
values);
查询数据:
代码案例:
SQLiteDatabase db=mDbHelper.getReadableDatabase();
String[] projection={FeedEntry._ID,FeedEntry.COLUMN_NAME_TITLE,FeedEntry.COLUMN_NAME_UPDATED};
String sortOrder=FeedEntry.COLUMN_NAME_UPDATED+"DESC";
Cursor c=db.query{
FeedEntry.TABLE_NAME,//表名
projection,//要查询的列名
selection,//查询条件
selectionArgs,//查询条件的参数
null,//分组条件
null,//分组条件的参数
sortOrder,//排序条件
limit//分页条件
};
在读数值之前,必须要调用move方法,首先调用moveToFirst()方法,游标到了第一个位置,取值就用get()方法,getString(),getLong()....但是get方法要传入index做参数,这个参数可以通过getColumnIndex()和getColumnIndexThrow()方法获取
//指针置首
cursor.moveToFirst();
//根据给定的列名,取值
long itemld=cursor.getLong{
cursor.getColumnIndexOrThrow(FeedReaderContract.FeedEntry_ID)
};
删除数据:
要是想删除一个表里的行,就要提供筛选标准来确定要删除的行。
数据库API提供一个机制来创建筛选标准,来防止SQL注入攻击。
//定义查询的WHERE部分
String selection=FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID+"LIKE?";
//Specify arguments in placeholder order.
String[] selectionArgs={String.valueOf(rowld)};
//组装SQL语句
//delete()方法中
参数1:表名
参数2:WHERE语句
参数3:要查的字段
db.delete{table_name,selection,selectionArgs);
更新数据:
SQLiteDatabase db=mDbHelper.getReadableDatabase();
//列新的值
ContentValues values=new ContentValues();
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE,title);
//根据ID,确定需要update的列
String selection=FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID+"LIKE?";
String[] selelectionArgs={String.valueOf(rowld)};
//执行update
int count=db.update{
FeedReaderDbHelper.FeedEntry.TABLE_NAME,
values,
selection,
selectionArgs);
使用原生SQL语句:
通过databaseHelper.getWritableDatabase()或getReadableDatabase()获取SQLiteDatabase对象后;
插入数据:
db.execSQL("insert into person(name,age)values(?,?)",new Object[]{"hhj",20});
查询数据:
Cursor cursor=db.rawQuery("select * from person where name like ? and age=?",new String[]{"%ne%","20"});
删除数据:
db.execSQL("delete from person where personid=2");
更新数据:
db.execSQL("update person set name='hhj',age=20 where personid=1"};

浙公网安备 33010602011771号