/*
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?iv 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 below
*/
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 Databasehelper(Context context) {
super(context, Dname, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE "+table_name+" ("+col1+" varchar(25), " +
col2+" varchar(10), " +col3+" float(5,3), " +
col4 +" varchar(255));");
db.execSQL("CREATE TABLE "+table2_name+" ("+col12+" float(10,2) NOT NULL DEFAULT '0.0');");
}
@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;
}
}
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????????????????????????????????????????????????????????????????????????????????????????????????
Aucun commentaire:
Enregistrer un commentaire