Reputation: 3118
My app keep crashing when I try to add a new row to a table (which then calls onCreate
because it doesn't exist yet). Line 40 is db.execSQL(CREATE_SESSIONS_TABLE_QUERY);
My assumption so far is that there is something wrong with the query string, but I can't figure out what it is.
This is the class that manages the database:
public class PBMDatabaseOperations extends SQLiteOpenHelper{
private static final int database_version = 1;
private String CREATE_SESSIONS_TABLE_QUERY = "CREATE_TABLE " + PBMDatabaseContract.SessionTable.TABLE_NAME + "("
+ PBMDatabaseContract.SessionTable.COLUMN_NAME_ID + " INT PRIMARY KEY AUTOINCREMENT, "
+ PBMDatabaseContract.SessionTable.COLUMN_NAME_SESSION_TYPE + " TEXT, "
+ PBMDatabaseContract.SessionTable.COLUMN_NAME_STAKE_LEVEL + " TEXT, "
+ PBMDatabaseContract.SessionTable.COLUMN_NAME_LIMIT_TYPE + " TEXT, "
+ PBMDatabaseContract.SessionTable.COLUMN_NAME_GAME_TYPE + " TEXT, "
+ PBMDatabaseContract.SessionTable.COLUMN_NAME_LOCATION + " TEXT, "
+ PBMDatabaseContract.SessionTable.COLUMN_NAME_START_TIME + " TEXT, "
+ PBMDatabaseContract.SessionTable.COLUMN_NAME_END_TIME + " TEXT, "
+ PBMDatabaseContract.SessionTable.COLUMN_NAME_BUY_IN + " INT, "
+ PBMDatabaseContract.SessionTable.COLUMN_NAME_CASH_OUT + " INT, "
+ PBMDatabaseContract.SessionTable.COLUMN_NAME_NUM_OF_PLAYERS + " INT, "
+ PBMDatabaseContract.SessionTable.COLUMN_NAME_POSITION_PLACED + " INT, "
+ PBMDatabaseContract.SessionTable.COLUMN_NAME_PLACES_PAID + " INT, "
+ PBMDatabaseContract.SessionTable.COLUMN_NAME_IS_COMPLETED + " INT"
+ ");";
public PBMDatabaseOperations(Context context) {
super(context, PBMDatabaseContract.DATABASE_NAME, null, database_version);
Log.d("Database operations", "Database created");
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_SESSIONS_TABLE_QUERY);
Log.d("Database operations", "Table created");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void putNewCashGame(PBMDatabaseOperations dbop, String stake, String limit, String gameType, String location, String startTime, int buyIn){
SQLiteDatabase sqdb = dbop.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(PBMDatabaseContract.SessionTable.COLUMN_NAME_STAKE_LEVEL, stake);
cv.put(PBMDatabaseContract.SessionTable.COLUMN_NAME_LIMIT_TYPE, limit);
cv.put(PBMDatabaseContract.SessionTable.COLUMN_NAME_GAME_TYPE, gameType);
cv.put(PBMDatabaseContract.SessionTable.COLUMN_NAME_LOCATION, location);
cv.put(PBMDatabaseContract.SessionTable.COLUMN_NAME_START_TIME, startTime);
cv.put(PBMDatabaseContract.SessionTable.COLUMN_NAME_BUY_IN, buyIn);
cv.put(PBMDatabaseContract.SessionTable.COLUMN_NAME_CASH_OUT, 0);
cv.put(PBMDatabaseContract.SessionTable.COLUMN_NAME_IS_COMPLETED, 0);
long k = sqdb.insert(PBMDatabaseContract.SessionTable.TABLE_NAME, null, cv);
Log.d("Database operations", "new row inserted to table " + PBMDatabaseContract.SessionTable.TABLE_NAME);
}
}
This is the method that is calling the method to add a new row:
createSession.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (buyIn.getText().toString().equals("")) {
Toast.makeText(v.getContext(), "The Buy In Field Cannot Be Empty", Toast.LENGTH_LONG).show();
}
else {
Session cg = new Session();
cg.setStakeLevel(stakes.getSelectedItem().toString());
cg.setLimitType(limitTypes.getSelectedItem().toString());
cg.setGameType(gameTypes.getSelectedItem().toString());
//TODO Location is currently a string needs to be a Location Object
//cg.setLocationName(locations.getSelectedItem().toString());
cg.setStartTime(startDate.getText().toString());
cg.setBuyIn(Integer.parseInt(buyIn.getText().toString()));
//add to database
PBMDatabaseOperations db = new PBMDatabaseOperations(C);
db.putNewCashGame(db, cg.getStakeLevel(), cg.getLimitType(), cg.getGameType(),locations.getSelectedItem().toString(), cg.getStartTime(), cg.getBuyIn());
Toast.makeText(v.getContext(), "New Cash Game Session Created", Toast.LENGTH_LONG).show();
finish();
}
}
});
and this is the logcat when I try to run it:
07-15 14:45:19.531 7107-7107/pokerbankrollmanager.com.pokerbankrollmanager D/Database operations﹕ Database created
07-15 14:45:19.534 7107-7107/pokerbankrollmanager.com.pokerbankrollmanager E/SQLiteLog﹕ (1) near "CREATE_TABLE": syntax error
07-15 14:45:19.535 7107-7107/pokerbankrollmanager.com.pokerbankrollmanager D/AndroidRuntime﹕ Shutting down VM
07-15 14:45:19.535 7107-7107/pokerbankrollmanager.com.pokerbankrollmanager E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: pokerbankrollmanager.com.pokerbankrollmanager, PID: 7107
android.database.sqlite.SQLiteException: near "CREATE_TABLE": syntax error (code 1): , while compiling: CREATE_TABLE sessions(id INT PRIMARY KEY AUTOINCREMENT, session_type TEXT, stake_level TEXT, limit_type TEXT, game_type TEXT, location TEXT, start_time TEXT, end_time TEXT, buy_in INT, cash_out INT, num_of_players INT, position_placed INT, places_paid INT, is_completed INT);
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
at pokerbankrollmanager.com.pokerbankrollmanager.PBMDatabaseOperations.onCreate(PBMDatabaseOperations.java:40)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at pokerbankrollmanager.com.pokerbankrollmanager.PBMDatabaseOperations.putNewCashGame(PBMDatabaseOperations.java:51)
at pokerbankrollmanager.com.pokerbankrollmanager.AddCashGame$2.onClick(AddCashGame.java:183)
at android.view.View.performClick(View.java:5199)
at android.view.View$PerformClick.run(View.java:21155)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5415)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:725)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:615)
Upvotes: 0
Views: 241
Reputation: 11038
You have the word CREATE_TABLE
when it should be two words CREATE TABLE
.
Upvotes: 3
Reputation: 3853
Change CREATE_SESSIONS_TABLE_QUERY = "CREATE_TABLE " for
CREATE_SESSIONS_TABLE_QUERY = "CREATE TABLE "
https://www.sqlite.org/lang_createtable.html
Upvotes: 1
Reputation: 3430
From the stack,I guess the space is missing between the table name and the ( brace.
CREATE_TABLE sessions(id INT PRIMARY KEY AUTOINCREMENT, session_type TEXT, stake_level TEXT, limit_type TEXT, game_type TEXT, location TEXT, start_time TEXT, end_time TEXT, buy_in INT, cash_out INT, num_of_players INT, position_placed INT, places_paid INT, is_completed INT);
Use the following code:
private String CREATE_SESSIONS_TABLE_QUERY = "CREATE_TABLE " +
PBMDatabaseContract.SessionTable.TABLE_NAME + " ("
Upvotes: 0