我想制作一个简单的 Android 游戏(如测验),在SQLiteDatabase
那里将保存玩家的结果(结果和名称)。但是使用此代码会出现错误。
请帮助我理解我做错了什么。先感谢您 :)。
public class forth extends Activity {
DBHelper dbHelper;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.forth);
dbHelper = new DBHelper(this);
TextView tvResult = (TextView) findViewById(R.id.tvResult);
TextView tvResults = (TextView) findViewById(R.id.tvResults);
Intent intent = getIntent();
String name = intent.getStringExtra("name");
String points = intent.getIntExtra("balance", 0);
ContentValues cv = new ContentValues();
SQLiteDatabase db = dbHelper.getWritableDatabase();
cv.put("points", points);
cv.put("name", name);
db.insert("mytable", null, cv);
Cursor c = db.query("mytable", null, null, null, null, null, null);
int PointsColIndex = c.getColumnIndex("points");
int nameColIndex = c.getColumnIndex("name");
tvResults.setText(c.getString(nameColIndex) + " " +c.getInt(PointsColIndex));
}
class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, "myDB", null, 1);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table mytable ("
+ "balance integer primary key autoincrement,"
+ "name text,"
+ "surname text" + ");");
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
}