Android 如何在SQLite数据库中添加更多表
我在android的SQLite数据库中创建两个以上的表时遇到了问题。 我想在我的应用程序的SQLite数据库中再添加两个表。 当我尝试添加更多表时,会显示一个错误意外标记。 这是我的Database.classAndroid 如何在SQLite数据库中添加更多表,android,database,sqlite,android-sqlite,Android,Database,Sqlite,Android Sqlite,我在android的SQLite数据库中创建两个以上的表时遇到了问题。 我想在我的应用程序的SQLite数据库中再添加两个表。 当我尝试添加更多表时,会显示一个错误意外标记。 这是我的Database.class public class DBForms { public static final int FORMS = 0 ; public static final int ADMITCARDS = 1; private FormHelper mHelper; private SQLiteD
public class DBForms {
public static final int FORMS = 0 ;
public static final int ADMITCARDS = 1;
private FormHelper mHelper;
private SQLiteDatabase mDatabase;
public DBForms (Context context){
mHelper = new FormHelper(context);
mDatabase = mHelper.getWritableDatabase();
}
public void insertForms(int table, ArrayList<FormItem> listForms,boolean clearPrevious){
if (clearPrevious){
deleteForms(table);
}
//create a sql prepared statement
String sql = "INSERT INTO " + (table == FORMS ? FormHelper.TABLE_FORMS : FormHelper.TABLE_ADMITCARDS) + " VALUES (?,?,?,?,?,?,?);";
//compile the statement and start a transaction
SQLiteStatement statement = mDatabase.compileStatement(sql);
mDatabase.beginTransaction();
for (int i = 0; i < listForms.size(); i++){
FormItem currentForm = listForms.get(i);
statement.clearBindings();
//for a given column index, simply bind the data to be put inside that index
statement.bindString(2,currentForm.getTitle());
statement.bindLong(3,currentForm.getLastdate() == null ? -1 : currentForm.getLastdate().getTime());
statement.bindString(4,currentForm.getCategory());
statement.bindString(5,currentForm.getDetails());
statement.bindString(6,currentForm.getProfilePic());
statement.bindString(7,currentForm.getUrl());
statement.execute();
}
//set the transaction as successful and end the transaction
L.m("inserting entries " + listForms.size() + new Date(System.currentTimeMillis()));
mDatabase.setTransactionSuccessful();
mDatabase.endTransaction();
}
public ArrayList<FormItem> readForms(int table){
ArrayList<FormItem> listForms = new ArrayList<>();
//get a list of columns to be retrieved, we need all of them
String[] columns = {FormHelper.COLUMN_UID,
FormHelper.COLUMN_TITLE,
FormHelper.COLUMN_LASTDATE,
FormHelper.COLUMN_CATEGORY,
FormHelper.COLUMN_DETAILS,
FormHelper.COLUMN_PROFILEPIC,
FormHelper.COLUMN_URL,
};
Cursor cursor = mDatabase.query((table == FORMS ? FormHelper.TABLE_FORMS : FormHelper.TABLE_ADMITCARDS), columns, null, null, null, null, null);
if (cursor != null && cursor.moveToFirst()){
L.m("loading entries " + cursor.getCount() + new Date(System.currentTimeMillis()));
do {
FormItem formItem = new FormItem();
formItem.setTitle(cursor.getString(cursor.getColumnIndex(FormHelper.COLUMN_TITLE)));
long lastdateMilliseconds = cursor.getLong(cursor.getColumnIndex(FormHelper.COLUMN_LASTDATE));
formItem.setLastdate(lastdateMilliseconds != -1 ? new Date(lastdateMilliseconds): null);
formItem.setCategory(cursor.getString(cursor.getColumnIndex(FormHelper.COLUMN_CATEGORY)));
formItem.setDetails(cursor.getString(cursor.getColumnIndex(FormHelper.COLUMN_DETAILS)));
formItem.setProfilePic(cursor.getString(cursor.getColumnIndex(FormHelper.COLUMN_PROFILEPIC)));
formItem.setUrl(cursor.getString(cursor.getColumnIndex(FormHelper.COLUMN_URL)));
listForms.add(formItem);
}
while (cursor.moveToNext());
}
return listForms;
}
public void deleteForms(int table){
mDatabase.delete((table == FORMS ? FormHelper.TABLE_FORMS : FormHelper.TABLE_ADMITCARDS), null, null);
}
private static class FormHelper extends SQLiteOpenHelper {
public static final String TABLE_ADMITCARDS = "admitcards";
public static final String TABLE_FORMS = "forms";
public static final String COLUMN_UID ="id";
public static final String COLUMN_TITLE ="title";
public static final String COLUMN_LASTDATE ="lastdate";
public static final String COLUMN_CATEGORY ="category";
public static final String COLUMN_DETAILS ="details";
public static final String COLUMN_PROFILEPIC ="profilePic";
public static final String COLUMN_URL ="url";
private static final String CREATE_TABLE_FORM = "CREATE TABLE "+ TABLE_FORMS + " (" +
COLUMN_UID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
COLUMN_TITLE + " TEXT," +
COLUMN_LASTDATE + " INTEGER," +
COLUMN_CATEGORY + " TEXT," +
COLUMN_DETAILS + " TEXT," +
COLUMN_PROFILEPIC+ " TEXT," +
COLUMN_URL+ " TEXT" +
");";
private static final String CREATE_TABLE_ADMITCARDS = "CREATE TABLE "+ TABLE_ADMITCARDS + " (" +
COLUMN_UID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
COLUMN_TITLE + " TEXT," +
COLUMN_LASTDATE + " INTEGER," +
COLUMN_CATEGORY + " TEXT," +
COLUMN_DETAILS + " TEXT," +
COLUMN_PROFILEPIC+ " TEXT," +
COLUMN_URL+ " TEXT" +
");";
private static final String DB_NAME = "forms_db";
private static final int DB_VERSION = 1;
private Context mContext;
public FormHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
mContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(CREATE_TABLE_FORM);
db.execSQL(CREATE_TABLE_ADMITCARDS);
L.m("create table forms executed");
} catch (SQLiteException exception) {
L.t(mContext, exception + "");
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try {
L.m("upgrade table form executed");
db.execSQL(" DROP TABLE " + TABLE_FORMS + " IF EXISTS;");
db.execSQL(" DROP TABLE " + TABLE_ADMITCARDS + " IF EXISTS;");
onCreate(db);
} catch (SQLiteException exception) {
L.t(mContext, exception + "SQLiteException");
}
}
}
}
公共类DBForms{
公共静态最终整数形式=0;
公共静态最终int ADMINTCARDS=1;
私人FormHelper mHelper;
私有SQLITE数据库mDatabase;
公共DBForms(上下文){
mHelper=新的FormHelper(上下文);
mDatabase=mHelper.getWritableDatabase();
}
公共void insertForms(int表、ArrayList列表表单、布尔clearPrevious){
如果(清除上一个){
删除表格(表格);
}
//创建sql准备的语句
字符串sql=“插入到”+(表==表单?FormHelper.table_表单:FormHelper.table_ADMITCARDS)+“值(?,,,,,,,,,?);”;
//编译语句并启动事务
SQLiteStatement=mDatabase.compileStatement(sql);
mDatabase.beginTransaction();
对于(int i=0;iprivate static final int DB_VERSION = 2;
db.execSQL(" DROP TABLE " + TABLE_FORMS + " IF EXISTS;");