Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert in SQLite Database android

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);
like image 807
Richard Tunner Avatar asked Nov 02 '12 08:11

Richard Tunner


People also ask

Which of the following is used to insert a data into the database in Android?

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.

Which of the function is called to execute query in Android SQLite?

SQLiteDatabase has methods to create, delete, execute SQL commands, and perform other common database management tasks.


1 Answers

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.

like image 196
Olaf Dietsche Avatar answered Oct 16 '22 08:10

Olaf Dietsche