2013-09-29 66 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

非常感谢:) :) –