Yoshidk
Yoshidk

Reputation: 459

two tables in a SQLite database

I want to add another table to my database, but the App crash and i get this error:

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

The code was working when i had only one table, and i used the same method to create the second table, the crash occurs shortly after this code is executed:

db.execSQL(DATABASE_STATS_CREATE);

Here is my class:

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

Upvotes: 0

Views: 106

Answers (2)

CL.
CL.

Reputation: 180260

SELECT is a keyword.

To use it in SQL commands, you have to quote it:

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

Upvotes: 0

mharper
mharper

Reputation: 3272

Probably a bad idea to name a column "select"

public static final String KEY_SELECTION = "select";

Try changing it to something that isn't a SQL reserved word, e. g.

public static final String KEY_SELECTION = "selekt";

Ghetto, I know, but it should work.

Upvotes: 2

Related Questions