public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DBNAME = "temp.sqlite";
public static String name;
public static final String DBLOCATION = "/data/data/my_packge_name/databases";
public Context mContext;
public SQLiteDatabase mDatabase;
public DatabaseHelper(Context context) {
super(context,DBNAME,null,1);
this.mContext=context;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void openDatabase(){
String dbPath = mContext.getDatabasePath(DBNAME).getPath();
if( mDatabase != null && mDatabase.isOpen() ) {
return;
}
mDatabase = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);
}
public String contentsLoad() {
openDatabase();
SQLiteDatabase mDB = mDatabase;
int sls=1;
Cursor cursor= mDB.rawQuery("SELECT * FROM my WHERE sl="+sls,null);
name=new String(cursor.getString(1));
closeDatabase();
return name;
}
public void closeDatabase() {
if (mDatabase!=null) {
mDatabase.close();
}
}
}
没有数据返回。
I thing错误发生在下面部分的contentsLoad()函数中,但是我完全不知道错误是什么
int sls=1;
Cursor cursor= mDB.rawQuery("SELECT * FROM my WHERE sl="+sls,null);
name=new String(cursor.getString(1));
我需要返回名称字符串。这怎么可能?
运行此功能并关闭应用程序时发生错误
答案 0 :(得分:2)
此行出现错误....光标游标= mDB.rawQuery(“SELECT * FROM my WHERE sl =”+ sls,null); name = new String(cursor.getString(1));
在从光标读取数据之前,您需要将其放在有效行上:
Cursor cursor= mDB.rawQuery("SELECT * FROM my WHERE sl="+sls,null);
if (cursor.moveToFirst()) {
name=new String(cursor.getString(1));
}
这将是代码崩溃的原因之一。可能还有其他人;用stacktrace开始调试。在这样的问题中也包括堆栈跟踪。