0

我想制作一个简单的 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) {

        }
      }   
     }
4

2 回答 2

0

为什么不使用 aBundle object来检索与intent. 我认为你应该这样做。

于 2013-09-29T17:03:05.680 回答
0

Tren,问题是我们看不到哪里有错误……你能加载 LogCat 吗?但尝试从意图中获取信息,如下所示:

Intent intent = new Intent();
    intent = getIntent();
    Bundle bundle = intent.getExtras();
        language = bundle.getString("Language");

也在查询中尝试使用db.query(dbHelper.YOUR_TABLE_NAME, null, null, null, null, null, null);

填写升级...让我给您发送一个很棒的 SQLITE 教程的链接...关注它...值得

http://www.vogella.com/articles/AndroidSQLite/article.html

试试看,还:

http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

我想你会找到你的答案和更好的方法来构建你的 SQL 表

于 2013-09-29T17:13:04.347 回答