Sqlite所作增删改查示例

前端之家收集整理的这篇文章主要介绍了Sqlite所作增删改查示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package com.db.imgfornote;
import android.content.ContentValues;
android.content.Context;
android.database.Cursor;
android.database.sqlite.sqliteDatabase;
android.database.sqlite.sqliteDatabase.CursorFactory;
android.database.sqlite.sqliteOpenHelper;
android.util.Log;
public class DBHelper extends sqliteOpenHelper {
final private static String mDbName= "imgfornote" ;
static int mDbVersion= 1 ;
static DBHelper mInstance= null ;
String mTUserPhoto= "UserPhoto" ;
String mCreatesqlForNoteClass= "create table if not exists NoteClass(classId integer primary key asc autoincrement,className NVARCHAR(100),rowTime timestamp default (datetime('now','localtime')))" ;
String mCreatesqlForUserPhoto= "create table if not exists UserPhoto(photoId integer primary key asc autoincrement,photoName VARCHAR(200),userPt VARCHAR(200),title VARCHAR(255),classId integer,content NVARCHAR(250),tag NVARCHAR(200),remark text,status integer default 0,182)!important; background:none!important">;
String[] mInsertsqlForNoteClass={ "insert into NoteClass(className) values('默认分类[私有]');" , "insert into NoteClass(className) values('读书笔记[私有]');" "insert into NoteClass(className) values('电子资料[公开]');" };
private DBHelper(Context context,CursorFactory factory) {
super (context,mDbName,factory,mDbVersion);
}
DBHelper GetInstance(Context context,sqliteDatabase.CursorFactory factory)
{
if (mInstance== ){
mInstance = new DBHelper(context,factory);
}
return mInstance;
}
@Override
void onCreate(sqliteDatabase db) {
// 创建表
db.execsql(mCreatesqlForNoteClass);
db.execsql(mCreatesqlForUserPhoto);
//初始化数据
for ( int i= 0 ;i<mInsertsqlForNoteClass.length;i++)
db.execsql(mInsertsqlForNoteClass[i]);
}
@Override
onUpgrade(sqliteDatabase db, oldVersion,182)!important; background:none!important">newVersion) {
// TODO Auto-generated method stub
}
Cursor ExecsqlForCursor(String sql,String[] selectionArgs){
sqliteDatabase db =getWritableDatabase();
Log.i( "ExecsqlForCursor" sql);
db.rawQuery(sql,selectionArgs);
}
Execsql(String sql){
try {
sqliteDatabase db =getWritableDatabase();
Execsql(sql,db);
} catch (Exception e){
Log.e( "Execsql Exception" e.printStackTrace();
}
}
Execsql(String sql,sqliteDatabase db ){
{
db.execsql(sql);
"Execsql" sql);
(Exception e){
e.printStackTrace();
}
}
//添加照片信息
long InsertUserPhoto(String photoName,String title){
sqliteDatabase db =getWritableDatabase();
ContentValues cv = ContentValues();
cv.put( "photoName" "title" db.insert(mTUserPhoto,cv);
}
//查询照片信息
public Cursor SearchPhoto( row,String sort){
Cursor cur = ;
{
String ord = (sort== || sort.toLowerCase().startsWith( "a" ))? "asc" : "desc" ;
String sql = "select * from UserPhoto order by photoId " +ord;
String[] args = {String.valueOf(row)};
(row> ){
sql += " limit ?" ;
else {
args= ;
}
cur = ExecsqlForCursor(sql,args);
(Exception e) {
@H_404_416@cur = ;
"SearchPhoto Exception" e.printStackTrace();
}
cur;
}
//修改照片信息
UpdateUserPhoto( photoId,182)!important; background:none!important">classId,String title,String content,String tag){
sqliteDatabase db =getWritableDatabase();
ContentValues();
"classId" "content" "tag" String[] args = {String.valueOf(photoId)};
db.update(mTUserPhoto,cv,56)!important; background:none!important">"photoId=?" }
//删除照片信息
DeleteUserPhoto( photoId){
sqliteDatabase db =getWritableDatabase();
String[] args = {String.valueOf(photoId)};
db.delete(mTUserPhoto,args);
}
}
原文链接:https://www.f2er.com/sqlite/200240.html

猜你在找的Sqlite相关文章