如何在SQLite数据库中添加更多表

时间:2016-05-30 19:14:42

标签: android database sqlite android-sqlite

我在Android中的SQLite数据库中创建两个以上的表时遇到问题。 我想在我的App的SQLite数据库中再添加两个表。 当我尝试添加更多表格时,会显示错误意外令牌。

这是我的Database.class

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");
        }

     }
  }

}    

2 个答案:

答案 0 :(得分:0)

db.execSQL(" DROP TABLE " + TABLE_FORMS + " IF EXISTS;");

这不是correct syntax; IF EXISTS必须在表名之前。

答案 1 :(得分:-1)

您应该将数据库版本值增加到2。

private static final int DB_VERSION = 2;