Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting values to SQLite table in Android

Tags:

I am new in android app developement. I tried to insert values to SQLite database through the below code;

public class cashbook extends Activity {      @Override     public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);         setContentView(R.layout.main);         SQLiteDatabase db;          db = openOrCreateDatabase(             "cashbookdata.db"             , SQLiteDatabase.CREATE_IF_NECESSARY             , null             );          final String Create_CashBook =             "CREATE TABLE CashData ("             + "id INTEGER PRIMARY KEY AUTOINCREMENT,"             + "Description TEXT,"              + "Amount REAL,"             + "Trans INTEGER,"              + "EntryDate TEXT);";          db.execSQL(Create_CashBook);           final String Insert_Data="INSERT INTO CashData VALUES(2,'Electricity',500,1,'04/06/2011')";         db.execSQL(Insert_Data); 

It shows error on emulator - The application CashBook has stopped unexpectedly.

The database and table created , but the value insertion is not working. Please help me to resolve this issue. Thanks.

like image 324
Sinoy Devassy Avatar asked Jun 06 '11 11:06

Sinoy Devassy


People also ask

How manually insert data in SQLite database in Android?

If you want to inset the data manually(fully graphical) do the following: Go to the DDMS perspective. File explorer (tab-menu) Locate your db (/data/data/com.

Where does SQLite store data in Android?

The Android SDK provides dedicated APIs that allow developers to use SQLite databases in their applications. The SQLite files are generally stored on the internal storage under /data/data/<packageName>/databases.


2 Answers

Seems odd to be inserting a value into an automatically incrementing field.

Also, have you tried the insert() method instead of execSQL?

ContentValues insertValues = new ContentValues(); insertValues.put("Description", "Electricity"); insertValues.put("Amount", 500); insertValues.put("Trans", 1); insertValues.put("EntryDate", "04/06/2011"); db.insert("CashData", null, insertValues); 
like image 164
Jon Avatar answered Sep 18 '22 10:09

Jon


okk this is fully working code edit it as per your requirement

public class TestProjectActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.main);     SQLiteDatabase db;     db = openOrCreateDatabase( "Temp.db"        , SQLiteDatabase.CREATE_IF_NECESSARY        , null          );     try {         final String CREATE_TABLE_CONTAIN = "CREATE TABLE IF NOT EXISTS tbl_Contain ("                 + "ID INTEGER primary key AUTOINCREMENT,"                 + "DESCRIPTION TEXT,"                 + "expirydate DATETIME,"                 + "AMOUNT TEXT,"                 + "TRNS TEXT," + "isdefault TEXT);";         db.execSQL(CREATE_TABLE_CONTAIN);         Toast.makeText(TestProjectActivity.this, "table created ", Toast.LENGTH_LONG).show();         String sql =             "INSERT or replace INTO tbl_Contain (DESCRIPTION, expirydate, AMOUNT, TRNS,isdefault) VALUES('this is','03/04/2005','5000','tran','y')" ;                        db.execSQL(sql);     }     catch (Exception e) {         Toast.makeText(TestProjectActivity.this, "ERROR "+e.toString(), Toast.LENGTH_LONG).show();   }}} 

Hope this is useful for you..
do not use TEXT for date field may be that was casing problem still getting problem let me know :)Pragna

like image 30
Android Avatar answered Sep 19 '22 10:09

Android