2013-09-29 62 views
0

我想做一個簡單的Android遊戲(如Quiz),其中SQLiteDatabase將會保存玩家的結果(結果和名稱)。但是使用這個代碼會給出一個錯誤。android SQLite數據庫查詢1

請幫我理解我做錯了什麼。先謝謝你 :)。

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) { 

     } 
     } 
    } 
+0

知道錯誤是有用的,人們這樣更影響來幫助你,特別是如果錯誤是人們熟悉。如果它有一個行號,則表示哪一行對應於該行號。 – BLaZuRE

回答

0

特倫,問題是我們不能看到哪裏出現錯誤...您可以加載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表

+0

非常感謝您 –

+0

無後顧之憂只要我幫助:) –

+0

但問題不在於Intent,問題在於SQLite。問題在於讀取或添加數據。雖然我認爲它與閱讀更相關。 –

0

你爲什麼不使用Bundle object檢索數據與intent磨磨蹭蹭。我認爲你應該這樣做。

+0

我很抱歉,我只是一個初學者。您能否在我的代碼中給我一個例子,當然如果它不難,謝謝。 :) –

+0

Intent intent = getIntent(); Bundle bun = intent.getExtras(); String str = bun.getString(「name」);嘗試運行這個。 –

+0

非常感謝:) :) –