我制作了一个新项目样本,但有些问题。
第1步:为数据库创建一个新类:
package com.isummation.customgallery;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
static final String dbName = "attach.db";
static final String bankTable = "bank";
public DatabaseHelper(Context context) {
super(context, dbName, null, 33);
}
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE admin (id INTEGER PRIMARY KEY, username TEXT, password TEXT, sec_question TEXT, sec_answer TEXT)");
db.execSQL("CREATE TABLE [bank] ([id] INTEGER PRIMARY KEY NULL,[old_file_address] TEXT NULL,[filename] TEXT NULL,[thumnail] TEXT NULL,[ext] TEXT NULL)");
db.execSQL("CREATE TABLE [enc] ([id] INTEGER NOT NULL PRIMARY KEY,[MasterKey] TEXT NULL)");
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
// db.execSQL("DROP TABLE IF EXISTS " + bankTable);
// db.execSQL("DROP TABLE IF EXISTS " + deptTable);
// db.execSQL("DROP TRIGGER IF EXISTS dept_id_trigger");
// db.execSQL("DROP TRIGGER IF EXISTS dept_id_trigger22");
// db.execSQL("DROP TRIGGER IF EXISTS fk_empdept_deptid");
// db.execSQL("DROP VIEW IF EXISTS " + viewEmps);
// onCreate(db);
}
public void InsertNew(String FileAddr, String FileName, String thumbnail,
String Ext) {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("insert into bank values('','" + FileAddr + "','" + FileName
+ "','" + thumbnail + "','" + Ext + "')");
// ContentValues cv = new ContentValues();
// cv.put("old_file_address", FileAddr);
// cv.put("filename", FileName);
// cv.put("thumbnail", thumbnail);
// cv.put("Ext", Ext);
// db.insert(bankTable, null, cv);
db.close();
}
public Cursor BindAll() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cur = db.rawQuery("select * from bank", new String[] {});
return cur;
}
}
第2步:在我的活动类中使用“InsertNew”函数
DatabaseHelper db = new DatabaseHelper(getApplicationContext());
db.InsertNew("test", "test", "test", "test");
bank
表有5列
我有5列,但我必须设置4列没有“ID”,因为这是主键。
问题:
Assets
文件夹中加载数据库?祝你好运