im new on Android. I have some trouble with the insert statement in the database, when im running the application the values have not been inserted. Please someone can help..
public class DatabaseAdapter extends SQLiteOpenHelper {
// Database attributes
public static final String DB_NAME = "MoneyManagerSystemTr";
public static final int DB_VERSION = 1;
// Table attributes
public static final String TABLE_ACCOUNT = "account_table";
//Account Table
public static final String KEY_BANKNAME ="bankname";
public static final String KEY_TYPE = "type";
public static final String KEY_ACCNUM = "accnum";
public static final String KEY_BALANCE = "balance";
public static final String KEY_EXPIRYDATE = "expirydate";
@Override
public void onCreate(SQLiteDatabase db) {
String AccountTable = "create table if not exists " + TABLE_ACCOUNT + " ( " + BaseColumns._ID + " integer primary key autoincrement, "
+ KEY_BANKNAME + " text not null, "
+ KEY_TYPE + " text, "
+ KEY_ACCNUM + " text, "
+ KEY_BALANCE + " text, "
+ KEY_EXPIRYDATE + " text);";
db.execSQL(AccountTable);
String ROW1 = "INSERT INTO " + TABLE_ACCOUNT + " Values ('Cash','','',0, '');";
db.execSQL(ROW1);
String ROW2 = "INSERT INTO " + TABLE_ACCOUNT + " Values ('Bank Account','','',0, '');";
db.execSQL(ROW2);
String ROW3 = "INSERT INTO " + TABLE_ACCOUNT + " Values ('Credit Card','','',0, '');";
db.execSQL(ROW3);
Android SQLite is the most preferred method of storing data in any android applications. Android SQLite is a relational database, therefore, It supports all types of relational database features.
SQLiteDatabase has methods to create, delete, execute SQL commands, and perform other common database management tasks.
Remove the semicolons from your insert statements and add quotes around 0:
String ROW1 = "INSERT INTO " + TABLE_ACCOUNT + " ("
+ KEY_BANKNAME + ", " + KEY_TYPE + ", "
+ KEY_ACCNUM + ", " + KEY_BALANCE + ", "
+ KEY_EXPIRYDATE + ") Values ('Cash', '', '', '0', '')";
db.execSQL(ROW1);
Better yet, heed the suggestion at execSQL() and use insert() instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With