import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class Databasehelper extends SQLiteOpenHelper {
public static final String Dname="info.db";
public static final String table_name="exp";
public static final String col1="CATEGORY";
public static final String col2="STATUS";
public static final String col3="AMT";
public static final String col4="DES";
public static final String table2_name="past";
public static final String col12="past_exp";
public static final String CREATE_FIRST_TABLE =
"CREATE TABLE " + table_name + " (" +
col1 + " varchar(25), " +
col2 + " varchar(10), " +
col3 + " float(5,3), " +
col4 + " varchar(255));";
public static final String CREATE_SECOND_TABLE =
"CREATE TABLE " + table2_name + " (" +
col12 + " float(10,2) NOT NULL DEFAULT '0.0');";
public Databasehelper(Context context) {
super(context, Dname, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(CREATE_FIRST_TABLE);
db.execSQL(CREATE_SECOND_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS "+table_name);
onCreate(db);
db.execSQL("DROP TABLE IF EXISTS "+table2_name);
onCreate(db);
}
public boolean insertData(String category,String status,double amt,
String des) {
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(col1, category);
cv.put(col2, status);
cv.put(col3, amt);
cv.put(col4, des);
long result=db.insert(table_name, null, cv);
if (result==(-1)) {
return false;
} else {
return true;
}
}
public boolean insertData2(float exp) {
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(col12, exp);
long result=db.insert(table2_name, null, cv);
if (result==(-1)) {
return false;
} else {
return true;
}
}
public Cursor getData() {
SQLiteDatabase db=this.getWritableDatabase();
Cursor c=db.rawQuery("select * from "+table_name, null);
return c;
}
public Cursor getData2() {
SQLiteDatabase db=this.getWritableDatabase();
Cursor c=db.rawQuery("select * from "+table2_name, null);
return c;
}
}
I need to make two tables-exp and past. I have created the two tables using exec in the onCreate(Sqlitdatabase db)
method, but the second table is not getting created and the app crashes. When is the onCreate
called exactly? I have created 2 databasehelpers in 2 different classes and using them one after the other. This is my database helper class... How do i make 2 tables using the same onCreate
method in the database helper class that is given above
Do we need 2 database helpers for creating 2 tables or 1 is sufficient?
Please reply as soon as possible. How do i make 2 tables using the same onCreate
method in the database helper class?
logcat error:
09-12 20:33:19.178: E/AndroidRuntime(21084): Caused by: android.database.sqlite.SQLiteException: no such table: past (code 1): , while compiling: select * from past
09-12 20:33:19.178: E/AndroidRuntime(21084): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
09-12 20:33:19.178: E/AndroidRuntime(21084): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
09-12 20:33:19.178: E/AndroidRuntime(21084): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
09-12 20:33:19.178: E/AndroidRuntime(21084): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
09-12 20:33:19.178: E/AndroidRuntime(21084): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
09-12 20:33:19.178: E/AndroidRuntime(21084): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
09-12 20:33:19.178: E/AndroidRuntime(21084): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
09-12 20:33:19.178: E/AndroidRuntime(21084): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
09-12 20:33:19.178: E/AndroidRuntime(21084): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)