如果不存在,我正试图保存得分。如果它可用,那么我需要从数据库中获得最高分。
SQLiteDatabase db= openOrCreateDatabase("tryitdb", MODE_PRIVATE, null);
String sql= "create table if not exists tabletry(score int)";
db.execSQL(sql);
db.close();
tv1= (TextView) findViewById(R.id.textView1);
et1= (EditText) findViewById(R.id.editText1);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
getsc();
}
});
};
public void getsc() {
SQLiteDatabase db= openOrCreateDatabase("tryitdb", MODE_PRIVATE, null);
String sql= "select * from tabletry";
Cursor c1= db.rawQuery(sql, null);
if(c1.isNull(0)){
init();
}
else{
int i1= c1.getColumnIndex("score");
String l= c1.getString(i1);
int old= Integer.valueOf(l);
int str= Integer.valueOf(et1.getText().toString());
if(old>=str){
tv1.setText(old);
}
else{
tv1.setText(str);
}
}
}
public void init() {
SQLiteDatabase db= openOrCreateDatabase("tryitdb", MODE_PRIVATE, null);
String string= "insert into tabletry values(?,?)";
Object[] o= new Object[1];
o[0] = et1.getText().toString();
db.execSQL(string,o);
db.close();
}
stacktrace告诉我没有像table这样的表。 还给出了特定行的错误
Cursor c1= db.rawQuery(sql, null);
请建议修复此问题。感谢。
答案 0 :(得分:2)
请管理光标。
if (cursor != null && cursor.getCount() > 0) {
c.moveToFirst();
// your logic goes here
} else {
Toast.makeText(getApplicationContext(), "No Record Found", 1000).show();
}
如果您有多个数据,那么您可以尝试
if (cursor != null && cursor.getCount() > 0) {
if (cur.moveToFirst()) {
do {
cur.getString(0); //any thing what you want to fetch
} while (cur.moveToNext());
}}
答案 1 :(得分:0)
更改>> db.equals(sql);
至db.execSQL(sql);