1.建立一個類繼承SQLiteOpenHelper並重寫父類的onCreate()方法,添加一個構造方法。在onCreate方法中編寫建立表的語句。數據庫
public class DatabaseHelper extends SQLiteOpenHelper { public static final String PRODUCT_TABLE_NAME = "productNotupload"; public static final String USERNAME = "username"; public static final String PRODUCTNAME= "product_name"; public static final String RECORDTIME = "transact_date"; public static final String FILEPATH = "filepath"; public static final String TRADENO = "tradeno"; public static final String DATABASE_NAME = "jingan.db"; public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table " + PRODUCT_TABLE_NAME + "(" + USERNAME + " varchar(20) not null, " + PRODUCTNAME + " varchar(60) not null," + RECORDTIME + " varchar(20) not null," + FILEPATH + " varchar(200) not null," + TRADENO + " varchar(20) not null )"); }
2. 使用:在須要使用該表時,先初始化數據庫對象獲得操做權限,從而進行insert,delete , update,query操做。ide
DatabaseHelper databaseHelper = new DatabaseHelper(this); //獲取數據庫對象 mSqLiteDatabase = databaseHelper.getWritableDatabase(); //獲取寫權限 //添加 insert ContentValues contentValues = new ContentValues(); contentValues.put(DatabaseHelper.USERNAME, username); contentValues.put(DatabaseHelper.PRODUCTNAME, productName); contentValues.put(DatabaseHelper.RECORDTIME, recordTime); contentValues.put(DatabaseHelper.FILEPATH, filepath); contentValues.put(DatabaseHelper.TRADENO, tradeno); mSqLiteDatabase.insert(DatabaseHelper.PRODUCT_TABLE_NAME, null, contentValues); //刪除 delete mSqLiteDatabase.delete(DatabaseHelper.PRODUCT_TABLE_NAME, whereClauseString, whereArgsString); //查詢 query Cursor cursor = mSqLiteDatabase.query(DatabaseHelper.PRODUCT_TABLE_NAME, columns, null, null, null, null, null); while (cursor.moveToNext()) { String userName = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.USERNAME)); String productName = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.PRODUCTNAME)); String recordTime = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.RECORDTIME)); String filepath = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.FILEPATH)); String tradeno = cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.TRADENO)); UnVerifyModel unVerifyModel = new UnVerifyModel(userName,productName,recordTime,filepath,tradeno); lists.add(unVerifyModel); Log.e("-----------------------", ":" + userName + "|" + productName + "|" + recordTime + "|" + filepath + "|" + tradeno + "/n"); }