android sqlite 增删改[insert、up…

package com.db.imgfornote;

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.SQLiteOpenHelper;
import android.util.Log;

public class DBHelper extends SQLiteOpenHelper {
<wbr><wbr><wbr>final private static String mDbName="imgfornote";<br><wbr><wbr><wbr>final private static int mDbVersion=1;<br><wbr><wbr><wbr>private static DBHelper mInstance=null;<br><wbr><wbr><wbr>private final static String mTUserPhoto="UserPhoto";<br><wbr><wbr><wbr>final private static String mCreateSqlForNoteClass="create table if not exists NoteClass(classId integer primary key asc autoincrement,className NVARCHAR(100),rowTime timestamp default (datetime('now', 'localtime')))";<br><wbr><wbr><wbr>final private static 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,rowTime timestamp default (datetime('now', 'localtime')))";<br><wbr><wbr><wbr>final private static String[] mInsertSqlForNoteClass={"insert into NoteClass(className) values('默认分类[私有]');","insert into NoteClass(className) values('读书笔记[私有]');","insert into NoteClass(className) values('电子资料[公开]');"};<br><wbr><wbr><wbr>private DBHelper(Context context, CursorFactory factory) {<br><wbr><wbr><wbr><wbr><wbr><wbr>super(context, mDbName, factory, mDbVersion);<wbr><wbr><wbr><br><wbr><wbr><wbr>}<br><wbr><wbr><wbr><br><wbr><wbr><wbr>public static DBHelper GetInstance(Context context, SQLiteDatabase.CursorFactory factory)<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr>if(mInstance==null){<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>mInstance = new DBHelper(context,factory);<wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr><wbr><wbr>return mInstance;<br><wbr><wbr><wbr>}<br><br><wbr><wbr><wbr>@Override<br><wbr><wbr><wbr>public void onCreate(SQLiteDatabase db) {<br><wbr><wbr><wbr><wbr><wbr><wbr>// 创建表<br><wbr><wbr><wbr><wbr><wbr><wbr>db.execSQL(mCreateSqlForNoteClass);<wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr>db.execSQL(mCreateSqlForUserPhoto);<br><wbr><wbr><wbr><wbr><wbr><wbr>//初始化数据<br><wbr><wbr><wbr><wbr><wbr><wbr>for(int i=0;i&lt;mInsertSqlForNoteClass.length;i++)<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>db.execSQL(mInsertSqlForNoteClass[i]);<br><wbr><wbr><wbr>}<br><br><wbr><wbr><wbr>@Override<br><wbr><wbr><wbr>public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {<br><wbr><wbr><wbr><wbr><wbr><wbr>// TODO Auto-generated method stub<br><br><wbr><wbr><wbr>}<br><wbr><wbr><wbr><br><wbr><wbr><wbr>private Cursor ExecSQLForCursor(String sql, String[] selectionArgs){<br><wbr><wbr><wbr><wbr><wbr><wbr>SQLiteDatabase db =getWritableDatabase();<br><wbr><wbr><wbr><wbr><wbr><wbr>Log.i("ExecSQLForCursor",sql);<br><wbr><wbr><wbr><wbr><wbr><wbr>return db.rawQuery(sql, selectionArgs);<br><wbr><wbr><wbr>}<br><wbr><wbr><wbr>private void ExecSQL(String sql){<br><wbr><wbr><wbr><wbr><wbr><wbr>try{<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>SQLiteDatabase db =getWritableDatabase();<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>ExecSQL(sql,db);<br><wbr><wbr><wbr><wbr><wbr><wbr>}catch(Exception e){<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>Log.e("ExecSQL Exception",e.getMessage());<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>e.printStackTrace();<br><wbr><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr>}<br><wbr><wbr><wbr>private void ExecSQL(String sql,SQLiteDatabase db ){<br><wbr><wbr><wbr>try{<br><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

你可能感兴趣的:(android)