android sqlite query 查询,android SQLite DataBase查询1(android SQLite DataBase query 1)

android SQLite DataBase查询1(android SQLite DataBase query 1)

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

}

}

}

I want to make a simple Android game(like Quiz) where by SQLiteDatabase there would be saved the results of the players(results and names). But using this code it gives an Error.

Please help me to understand what I am doing wrong. Thank you in Advance :).

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

}

}

}

原文:https://stackoverflow.com/questions/19080741

更新时间:2020-01-02 19:15

最满意答案

特伦,问题是我们无法看到哪里有错误......你能加载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教程的链接...跟着它...值得它

尝试它并且:

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

Tren, the problem is we cant see where there error is... could you load LogCat? but try getting the info from the intent like so:

Intent intent = new Intent();

intent = getIntent();

Bundle bundle = intent.getExtras();

language = bundle.getString("Language");

also in query try to use db.query(dbHelper.YOUR_TABLE_NAME, null, null, null, null, null, null);

fill in the on upgrade... let me send you a link to a great SQLITE tutorial...follow it... its worth it

try it and also :

I think you'll find your answers and a better way to build your SQL table

2013-09-29

相关问答

内置的Android sqlite数据库可能太旧而无法支持此查询。 以下Android sqlite数据库库将支持此查询(已测试 ): https://github.com/liteglue/Android-sqlite-connector https://github.com/sqlcipher/android-database-sqlcipher 此外,一些较新版本的Android可能已更新sqlite。 以供参考: https://stackoverflow.com/a/4377116/12

...

你不断地使用表名。 它应该更像这样: int fav = 1;

Cursor c = sqliteDB.rawQuery("UPDATE "+ MyConstants.TABLE_NAME + " SET "+ MyConstants.ISFAV + " = "+fav+ " WHERE " + MyConstants.WORD_NAME + " = \""+word_name+"\"", null);

这应该给你一个看起来更像这样的结果查询: UPDATE words SET isfavor

...

检查撇号是否有效,但要避免这种情况,请使用选择参数? 运营商。 cursor = sqlDb.query(MYTABLE,

thecolumns, NAME + " LIKE ?", new String[]{"%" + name + "%"}, null, null, null);

Check if the apostrophes are valid, but to avoid this use the selection args with the ? operator. curso

...

特伦,问题是我们无法看到哪里有错误......你能加载LogCat吗? 但尝试从意图中获取信息,如下所示: Intent intent = new Intent();

intent = getIntent();

Bundle bundle = intent.getExtras();

language = bundle.getString("Language");

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

...

vehicle_implement LEFT JOIN vehicle ON vehicle_implement.vehicle_id = vehicle._id LEFT JOIN implement ON vehicle_implement.implement_id = implement._id LEFT JOIN vehicle_model ON vehicle.vehicle_model_id = vehicle_model._id LEFT JOIN vehicle_type ON v

...

使用这个片段: String table = "tblsample";

String selection = "DefaultId =? OR Name=?";

String[] selectionArgs = new String[]{"567"};

String[] projection = new String[]{"DefaultId","Name"}; // if you want to fetch only these two columns

在你的database.query之前

...

有SQL语法问题,您需要使用Cursor来检索查询结果,例如使用rawQuery() : String selectQuery = "SELECT lastchapter FROM Bookdetails WHERE bookpath=?";

Cursor c = db.rawQuery(selectQuery, new String[] { fileName });

if (c.moveToFirst()) {

temp_address = c.getString(c.getColumn

...

Indexing使我的查询速度提高了400倍 。 在这里,您将找到一个很好的教程: http : //www.tutorialspoint.com/sqlite/sqlite_indexes.htm 。 索引WHERE子句中涉及的所有字段以及用于joining表的所有字段。 Indexing made my queries about 400x faster. Here you'll find a nice tutorial: http://www.tutorialspoint.com/sqlit

...

像这样使用: String qry = "select a.*,b.* from table1 a,table2 b where a.field1=b.field1";

Cursor cur = db.rawQuery(qry,null);

use like this: String qry = "select a.*,b.* from table1 a,table2 b where a.field1=b.field1";

Cursor cur = db.rawQuery(qry,null);

//Declaration

SQLiteDatabase dbx;

ContentValues cv1;

EventDataSQLHelper eventsData;//object of class in which table is created

//on create

eventsData = new EventDataSQLHelper(this);

dbx= eventsData.getReadableDatabase();

cv1 = new ContentValues()

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值