我试图访问我的SQlite表中的所有值并将它们存储为整数值,但每次运行程序时它都会崩溃
当我点击按钮(在模拟器上或在我的手机上运行)时,它只是崩溃,而我希望看到textview更改文本(取决于我点击的按钮)
这是代码:
(main_activity)
int x = 0;
int y =0;
int[] stats = new int[]{};
@Override
public void onClick(View arg0) {
switch(arg0.getId()){
case R.id.button1:
x= 1;
Database db = new Database(this);
db.open();
db.createEntry(x, y);
stats = db.getStatistics();
db.close();
x= 0;
txt_view.setText("");
txt_view.setText(Integer.toString(stats[0]));
break;
case R.id.button2:
y=1;
Database db2 = new Database(this);
db2.open();
db2.createEntry(x, y);
stats = db2.getStatistics();
db2.close();
cravings = 0;
db2.getStatistics();
txt_view2.setText("");
txt_view2.setText(Integer.toString(stats[1]));
break;
}
}
这是我的数据库类
public class Database extends Activity{
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
public static final String DATABASE_NAME = "DB_NAME";
public static final String TABLE_NAME = "DB_TABLE";
public static final String NUM_SMOKES = "NUM_X";
private static final int DATABASE_VERSION = 1;
public static final String NUM_CRAVINGS = "NUM_Y";
public static final String CreateMyDatabase = "create table "+ TABLE_NAME +" ("+NUM_SMOKES+" integer default 0,"+NUM_CRAVINGS+" integer default 0)";
private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(CreateMyDatabase);
}
@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);
}}
public Database(Context c ){
ourContext = c;}
public Database open() throws SQLException{
ourHelper = new DbHelper(ourContext, DATABASE_NAME, null, DATABASE_VERSION);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public long createEntry(int x, int y){
ContentValues cv = new ContentValues();
cv.put(NUM_X, x);
cv.put(NUM_Y, y);
return ourDatabase.insert(DATABASE_NAME, null, cv);
}
public int[] getStatistics(){
String[] column = new String[]{NUM_X,NUM_Y};
Cursor c = ourDatabase.query(DATABASE_NAME, column, null, null, null,
null, null);
int x=0;
int y= 0;
int i_X= c.getColumnIndex(NUM_X);
int i_Y= c.getColumnIndex(NUM_Y);
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
y+= c.getInt(i_Y);
x+= c.getInt(i_X);
}
int[] X&Y= new int[]{x, y};
return X&Y;
}
感谢您的帮助!
答案 0 :(得分:0)
尝试插入如下值:
//In your model define method that returns content values for that table like
public class SomeModel{
private int field1;
public ContentValues getContentValues(){
ContentValues localContentValues = new ContentValues();
localContentValues.put("id", getId());
//Other values...
return localContentValues;
}
}
//And in sqliteopen handler do insert like this
//Pass variables to constructor
SomeModel model=new SomeModel(...);
ContentValues modelValues = model.getContentValues();
this.db.insert("SomeModel", null, modelValues );
答案 1 :(得分:0)
错误是“没有这样的表存在',我先前查看了它并完全错过了它,并修复了代码。现在它有效。问题是我试图通过db.insert(DATABASE_NAME);
插入而不是db.insert(TABLE_NAME);