Reputation: 3855
im having a weird crash when creating multiple SQLite tables...
the code works fine if i Comment Out the line where i execute CREATE_MOVEMENTS_TABLE.
The first table is created without problem, if i try to create both, my app crashes.
THE WEIRD THING IS: If i test the app on my Phone without creating the second table, it works fine, then ... if i test again (with the app installed), BUT with the two sql statements, (creating both tables), the App WORKS... The problem occurs when the app is not present at all in the phone
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "ClientsManager";
private static final String CLIENTS_TABLE = "Clients";
private static final String MOVEMENTS_TABLE = "Movements";
//Client Table Columns
private static final String KEY_ROWID = "numint";
private static final String KEY_NAME = "name";
private static final String KEY_LOCAL = "locality";
private static final String KEY_ADDR = "address";
private static final String KEY_BTTLP = "bottle_price";
private static final String KEY_PBLNC = "prev_balance";
private static final String KEY_BLNC = "balance";
private static final String KEY_BBTTLS = "bonif_bottles";
private static final String KEY_PBTTLS = "prev_bottles";
private static final String KEY_BTTLS = "bottles";
private static final String KEY_SYNC = "sync_date";
private static final String KEY_MODIF = "modif_date";
//Movements Table Columns
private static final String MKEY_ROWID = "numint";
private static final String MKEY_ID_MOVE = "move_id";
private static final String MKEY_CLIENT_ID = "client_id";
private static final String MKEY_PICKUP = "pickup";
private static final String MKEY_DELIVER = "deliver";
private static final String MKEY_PAYMENT = "payment";
private static final String MKEY_MOVE_DATE = "move_date";
private static final String MKEY_SYNC_DATE = "sync_date";
private static final String MKEY_FLAG_SYNC = "flag_sync";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String CREATE_CLIENTS_TABLE = "CREATE TABLE " + CLIENTS_TABLE + "("
+ KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
+ KEY_NAME + " TEXT NOT NULL, "
+ KEY_LOCAL + " TEXT NOT NULL, "
+ KEY_ADDR + " TEXT NOT NULL, "
+ KEY_BTTLP + " REAL NOT NULL, "
+ KEY_PBLNC + " REAL NOT NULL, "
+ KEY_BLNC + " REAL NOT NULL, "
+ KEY_BBTTLS + " INTEGER NOT NULL, "
+ KEY_PBTTLS + " INTEGER NOT NULL, "
+ KEY_BTTLS + " INTEGER NOT NULL, "
+ KEY_SYNC + " TEXT NOT NULL, "
+ KEY_MODIF + " TEXT NOT NULL )";
String CREATE_MOVEMENTS_TABLE = "CREATE TABLE " + MOVEMENTS_TABLE + "("
+ MKEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
+ MKEY_ID_MOVE + "TEXT NOT NULL"
+ MKEY_CLIENT_ID + " INTEGER NOT NULL, "
+ MKEY_PICKUP + " INTEGER NOT NULL, "
+ MKEY_DELIVER + " INTEGER NOT NULL, "
+ MKEY_PAYMENT + " REAL NOT NULL, "
+ MKEY_MOVE_DATE + " TEXT NOT NULL, "
+ MKEY_SYNC_DATE + " TEXT NOT NULL, "
+ MKEY_FLAG_SYNC + " INTEGER NOT NULL )";
sqLiteDatabase.execSQL(CREATE_CLIENTS_TABLE);
sqLiteDatabase.execSQL(CREATE_MOVEMENTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + CLIENTS_TABLE);
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + MOVEMENTS_TABLE);
onCreate(sqLiteDatabase);
}
Upvotes: 1
Views: 3211
Reputation: 14199
you are forgetting to put space and comma in +MKEY_ID_MOVE + "TEXT NOT NULL"
String CREATE_MOVEMENTS_TABLE = "CREATE TABLE " + MOVEMENTS_TABLE + "("
+ MKEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
+ MKEY_ID_MOVE + " TEXT NOT NULL,"
+ MKEY_CLIENT_ID + " INTEGER NOT NULL, "
+ MKEY_PICKUP + " INTEGER NOT NULL, "
+ MKEY_DELIVER + " INTEGER NOT NULL, "
+ MKEY_PAYMENT + " REAL NOT NULL, "
+ MKEY_MOVE_DATE + " TEXT NOT NULL, "
+ MKEY_SYNC_DATE + " TEXT NOT NULL, "
+ MKEY_FLAG_SYNC + " INTEGER NOT NULL )";
Also use try Catch for sqLiteDatabase.execSQL
try
{
sqLiteDatabase.execSQL(CREATE_CLIENTS_TABLE);
}
Catch(Exception e)
{
}
try
{
sqLiteDatabase.execSQL(CREATE_MOVEMENTS_TABLE);
}
Catch(Exception e)
{
}
Upvotes: 2