SQLite数据库中的两个表

时间:2013-06-03 18:29:31

标签: android sqlite

我想在我的数据库中添加另一个表,但是App崩溃了,我收到了这个错误:

E/SQLiteLog(1025): (1) near "select": syntax error

当我只有一个表时,代码正常工作,并且我使用相同的方法创建第二个表,在执行此代码后不久发生崩溃:

db.execSQL(DATABASE_STATS_CREATE);

这是我的班级:

BetsDbAdapter.java

public class BetsDbAdapter {

    /*-----------------BetList---------------------*/
    public static final String KEY_ROWID = "_id";
    public static final String KEY_MATCH = "match";
    public static final String KEY_TIME = "time";
    public static final String KEY_BOOKMAKERS = "bookmakers";
    public static final String KEY_ODDS1 = "odds1";

    /*-----------------Statistik---------------------*/
    public static final String KEY_SID = "_sid";
    public static final String KEY_SMATCH = "smatch";   
    public static final String KEY_SELECTION = "select";
    public static final String KEY_BETAMOUNT = "betamount";
    public static final String KEY_BODDS = "boods";

    private static final String TAG = "BetsDbAdapter";
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;

    private static final String DATABASE_NAME = "Bets.db";
    private static final String SQLITE_TABLE = "BetTable";
    private static final String SQLITE_STATS_TABLE = "Statistik";

    private static final int DATABASE_VERSION = 1;

    private final Context mCtx;

    private static final String DATABASE_CREATE = "CREATE TABLE if not exists "
            + SQLITE_TABLE + " (" + KEY_ROWID
            + " integer PRIMARY KEY autoincrement," + KEY_MATCH + ","
            + KEY_TIME + "," + KEY_BOOKMAKERS + "," + KEY_ODDS1 + ","
            + " UNIQUE (" + KEY_MATCH + "));";

    private static final String DATABASE_STATS_CREATE = "CREATE TABLE if not exists "
            + SQLITE_STATS_TABLE + " (" + KEY_SID
            + " integer PRIMARY KEY autoincrement," + KEY_SMATCH + ","
            + KEY_SELECTION + "," + KEY_BETAMOUNT + "," + KEY_BODDS + ","
            + " UNIQUE (" + KEY_SMATCH + "));";

    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            Log.w(TAG, DATABASE_CREATE);
            db.execSQL(DATABASE_CREATE);
            db.execSQL(DATABASE_STATS_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");            
            db.execSQL("DROP TABLE IF EXISTS " + SQLITE_TABLE);
            db.execSQL("DROP TABLE IF EXISTS " + SQLITE_STATS_TABLE);
            onCreate(db);
        }
    }

    public BetsDbAdapter(Context ctx) {
        this.mCtx = ctx;
        mDbHelper = new DatabaseHelper(mCtx);
    }

    public BetsDbAdapter open() throws SQLException {
        //mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        if (mDbHelper != null) {
            mDbHelper.close();
        }
    }

    public long createBets(String match, String time, String bookmakers,
            String odds1) {

        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_MATCH, match);
        initialValues.put(KEY_TIME, time);
        initialValues.put(KEY_BOOKMAKERS, bookmakers);
        initialValues.put(KEY_ODDS1, odds1);

        return mDb.insert(SQLITE_TABLE, null, initialValues);
    }

    public long statBets(String smatch, String select, String betamount,
            String bodds) {

        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_SMATCH, smatch);
        initialValues.put(KEY_SELECTION, select);
        initialValues.put(KEY_BETAMOUNT, betamount);
        initialValues.put(KEY_BODDS, bodds);

        return mDb.insert(SQLITE_STATS_TABLE, null, initialValues);
    }

2 个答案:

答案 0 :(得分:2)

将列命名为“select”

可能是一个坏主意
public static final String KEY_SELECTION = "select";

尝试将其更改为非SQL保留字的内容,例如:克。

public static final String KEY_SELECTION = "selekt";

少数民族居住区,我知道,但它应该有用。

答案 1 :(得分:0)

SELECTkeyword

要在SQL命令中使用它,您必须引用它:

... + ",\"" + KEY_SELECTION + "\"," + ...