Android SQLite:请求索引-1,大小为1
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Android SQLite:请求索引-1,大小为1,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4765字,纯文字阅读大概需要7分钟。
内容图文
![Android SQLite:请求索引-1,大小为1](/upload/InfoBanner/zyjiaocheng/910/684e48d386f242d7a34b65ee515b43ad.jpg)
我的应用程序需要一个数据库,但由于下面的错误,它会继续强制关闭.
它适用于我的一项活动,但不适用于另一项活动
数据库处理程序:
public class DBGestion {
private static final int VERSION_BDD = 1;
private static final String NOM_BDD = "cityquest.db";
private static final String TABLE_QUEST = "table_quest";
private static final String COL_ID = "ID";
private static final int NUM_COL_ID = 0;
private static final String COL_USERNAME = "UserName";
private static final int NUM_COL_USERNAME = 1;
private static final String COL_NIVEAU ="Niveau";
private static final int NUM_COL_NIVEAU = 2;
private static final String COL_NBRIGOLETTES ="NbRigolettes";
private static final int NUM_COL_NBRIGOLETTES = 3;
private SQLiteDatabase bdd;
private MaBaseSQLite maBaseSQLite; // MaBaseSQLite extends SQLiteOpenHelper
public DBGestion(Context context) {
maBaseSQLite = new MaBaseSQLite(context, NOM_BDD, null, VERSION_BDD);
}
public void open() {
bdd = maBaseSQLite.getWritableDatabase();
}
public void close() {
bdd.close();
}
public SQLiteDatabase getBDD() {
return bdd;
}
public long initDB(String userName){
ContentValues values = new ContentValues();
values.put(COL_USERNAME, userName);
values.put(COL_NIVEAU, 0);
values.put(COL_NBRIGOLETTES, 0);
return bdd.insert(TABLE_QUEST,null, values);
}
public int getNiveau() {
Cursor c = bdd.query(TABLE_QUEST, new String[] {COL_NIVEAU}, null, null, null, null, null);
int niveau = c.getInt(NUM_COL_NIVEAU);
return niveau;
}
public long setNiveau(int niveau) {
ContentValues values = new ContentValues();
values.put(COL_NIVEAU, niveau);
return bdd.update(TABLE_QUEST,values, COL_ID + " = 0", null);
}
}
这里是我使用get和set方法的代码(抱歉所有的法语变量名!).基本上我首先检查问题是否已经回答(getNiveau()== 1),如果没有,我检查答案,然后制作一些Widget INVISIBLE,并设置setNiveau(1)(不可能关闭()dbGestion 2次,因为按钮被隐藏)
在我尝试使用数据库之前,我没有遇到任何问题.
public class Question extends Activity {
[variable declaration]
DBGestion dbGestion;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.question_elisa);
Intent i = getIntent();
int format = i.getIntExtra(MainActivity.FORMAT,0);
dbGestion = new DBGestion(this);
[findViewById for all my variables]
if (format == 1){
dbGestion.open();
if (dbGestion.getNiveau() == 1) {
bonneReponse.setVisibility(View.VISIBLE);
[......]
dbGestion.close();
}
bouton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String reponse = new String("");
reponse = editText.getText().toString();
if(reponse.equalsIgnoreCase("poete") ||..){
bonneReponse.setVisibility(View.VISIBLE);
[...]
}
else {...}
dbGestion.setNiveau(1);
dbGestion.close();
}});
你知道问题出在哪里吗?
以下是我启动数据库的方法(在MainActivity onCreate方法中):
final DBGestion dbGestion = new DBGestion(this);
dbGestion.open();
dbGestion.initDB("Alex");
dbGestion.close();
这是我的SQLiteOpenHelper类:
public class MaBaseSQLite extends SQLiteOpenHelper {
private static final String TABLE_QUEST ="table_quest";
private static final String COL_ID ="ID";
private static final String COL_NIVEAU ="Niveau";
private static final String COL_USERNAME ="UserName";
private static final String COL_NBRIGOLETTES ="NbRigolettes";
private static final String CREATE_DB = "CREATE TABLE " + TABLE_QUEST + " (" + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COL_USERNAME + " TEXT NOT NULL, " + COL_NIVEAU + " TEXT NOT NULL, " + COL_NBRIGOLETTES + " TEXT NOT NULL); ";
public MaBaseSQLite(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_DB);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE" + TABLE_QUEST + ";");
onCreate(db);
}
}
链接到Logcat:http://pastebin.com/ds9bKFcS
再次感谢..
解决方法:
从错误代码中,您可以将其追溯到这段代码:
public int getNiveau() {
Cursor c = bdd.query(TABLE_QUEST, new String[] {COL_NIVEAU}, null, null, null, null, null);
int niveau = c.getInt(NUM_COL_NIVEAU);
return niveau;
}
首先,当您从数据库中获取Cursor时,检查光标并将其移动到相应的行是您的工作.例如,在您的情况下,如果知道您的团队只有一行,您可以这样做:
public int getNiveau() {
Cursor c = bdd.query(TABLE_QUEST,new String[] { COL_NIVEAU},null,null,null,null,null);
if(c.getCount() == 1) {
c.moveToFirst();
int niveau = c.getInt(c.getColumnIndexOrThrow(MaBaseSQLite.COL_NIVEAU));
c.close();
return niveau;
}
c.close();
return -1;
}
基本上我正在做的是检查我是否只有一个结果列,如果我移动到第一列然后在位置获得整数.您必须记住的另一件事是从列名称中获取Column索引.如果您决定更改数据库架构,则可以获得更好的可移植性.所有问题都可以通过位于此处的Cursor文档得到解答:http://developer.android.com/reference/android/database/Cursor.html
希望能回答你的所有问题.
内容总结
以上是互联网集市为您收集整理的Android SQLite:请求索引-1,大小为1全部内容,希望文章能够帮你解决Android SQLite:请求索引-1,大小为1所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。