I'm a beginner at android and sqllite as well. I have been trying to build a database which retrieves and shows entries like items ,price and time, Ive looked through the all the other topics but i just cant seem to find why does it crash.. Ive tried a LOT of ways to make it work but this is the code im trying to use
public class data extends SQLiteOpenHelper {
private static final String DATABASE_NAME ="items.db";
private static final String t="database created";
private static final int DATABASE_VERSION=1;
Context ct;
SQLiteDatabase db;
public data (Context ctx) {
super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
ct=ctx;
}
@Override
public void onCreate(SQLiteDatabase db)
{db.execSQL("CREATE TABLE record (_id primary key autoincrement,TIME text,ITEM text,TYPE text,AMT text)");
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(ct, t, duration);
toast.show();
}
@Override
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)
{db.execSQL("DROP TABLE IF EXISTS"+TABLE_NAME);
onCreate(db);
}
}
here is the class which I use to run the data class object
public class mainscreen extends Activity {
private data Data;
private static String[] cols={"_id","TIME","ITEM","TYPE","AMT"};
private static String ORDER_BY = TIME + "DESC";
private static int[] TO={R.id.textView5,R.id.textView3,R.id.textView,R.id.textView4,R.id.textView2};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainscreen);
Data = new data(this);
}
public void tryit(View view)
{
time t=new time();
data test =new data(this);
add(t.tostring(),"tomato", "200", "Food");
//Cursor cursor = getd();
// show(cursor);
}
private void add(String t,String item, String price,String type) {
SQLiteDatabase db = Data.getWritableDatabase();
db.execSQL("insert into "+TABLE_NAME+" values(null,'" + t + "','" + item + "','" + type + "','" + price + "')");
}
As of now Im simply trying to get the database to show some entries i add using add(); 1.It does not seem to run OnCreate at all with the Data class,(i used toast to try and see if the oncreate runs at all) 2.trying to use a SQLLITEDatabase object for getreadabledatabase/getwritabledatabase causes a crash 3.Even the simple execSQL("create******") method above is causing a crash. 4.The worst part is it ACTUALLY worked 2-3 times yesterday.It showed me 5-8 entries using the listview just as I wanted.and is crashing now sob . I just cant seem to find the problem!!
Im not too sure how to get a stackdump/trace so if needed please tell me how to get it . If anyone could tell me what Im doing wrong. Ive marked out the cursor/adapters as theres no point getting that to work if the database doesnt work
I use tryit() by a button to start the insert...and it crashes at SQLiteDatabase db = Data.getWritableDatabase(); I think the whole problem is because for some reason the database is not getting created , but even using the plain db.execSQL statement whether in onCreate or by using a db object in data class and execsql at constructor crashes the app.
Aucun commentaire:
Enregistrer un commentaire