我想在EditText中显示记录列的内容。所以为了达到这个目的,我在我的适配器中创建了这个函数,以获得一个与ID + Name对相对应的unqiue行
public Cursor queryNotes(int lectureID,String user){
String[] columns = new String[]{KEY_ID,USERNAME,LECTURE_NAME, LECTURE_DATE,LECTURE_NOTES};
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE,columns ,
KEY_ID+"=? AND "+USERNAME+"=?" , new String[] { String.valueOf(lectureID),String.valueOf(user) }, null, null, null,null);
return cursor;
}
然后在我的片段类中,我想设置文本,我做了这样的事情:
cursor = mySQLiteAdapter.queryNotes(position,name);
cursor.moveToPosition(0);
if(cursor!=null) {
do {
String previousNotes = cursor.getString(4);
shortnotes.setText(previousNotes);//I assume shortnotes is the EditText I want to set
}while(cursor.moveToNext());
}
cursor.close();
这是设置编辑文本的正确方法吗?请帮帮我
Logcat:
11-04 17:46:27.141:E / AndroidRuntime(9154):在writeCrashedAppName中,pkgName:com.example.loginphase 11-04 17:46:27.141:D / AndroidRuntime(9154):使用内容成功编写的文件:com.example.loginphase StringBuffer :; com.example.loginphase 11-04 17:46:27.141:E / AndroidRuntime(9154):致命异常:主要 11-04 17:46:27.141:E / AndroidRuntime(9154):进程:com.example.loginphase,PID:9154 11-04 17:46:27.141:E / AndroidRuntime(9154):android.database.sqlite.SQLiteException:没有这样的列:LECTURE_NOTES(代码1):,编译时:SELECT _id,USERNAME,LECTURE_NAME,LECTURE_DATE,LECTURE_NOTES FROM LECTURE_TABLE WHERE _id =? AND USERNAME =? 11-04 17:46:27.141:E / AndroidRuntime(9154):在android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 11-04 17:46:27.141:E / AndroidRuntime(9154):在android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) 11-04 17:46:27.141:E / AndroidRuntime(9154):在android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) 11-04 17:46:27.141:E / AndroidRuntime(9154):在android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 11-04 17:46:27.141:E / AndroidRuntime(9154):在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:58) 11-04 17:46:27.141:E / AndroidRuntime(9154):在android.database.sqlite.SQLiteQuery。(SQLiteQuery.java:37) 11-04 17:46:27.141:E / AndroidRuntime(9154):在android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44) 11-04 17:46:27.141:E / AndroidRuntime(9154):在android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314) 11-04 17:46:27.141:E / AndroidRuntime(9154):在android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161) 11-04 17:46:27.141:E / AndroidRuntime(9154):在android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032) 11-04 17:46:27.141:E / AndroidRuntime(9154):在android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1238) 11-04 17:46:27.141:E / AndroidRuntime(9154):at database.LecturesDatabaseAdapter.queryNotes(LecturesDatabaseAdapter.java:83) 11-04 17:46:27.141:E / AndroidRuntime(9154):at com.example.loginphase.RecordLectureFragment.onCreateView(RecordLectureFragment.java:34) 11-04 17:46:27.141:E / AndroidRuntime(9154):在android.app.Fragment.performCreateView(Fragment.java:1700) 11-04 17:46:27.141:E / AndroidRuntime(9154):在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890) 11-04 17:46:27.141:E / AndroidRuntime(9154):在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062) 11-04 17:46:27.141:E / AndroidRuntime(9154):在android.app.BackStackRecord.run(BackStackRecord.java:684) 11-04 17:46:27.141:E / AndroidRuntime(9154):在android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447) 11-04 17:46:27.141:E / AndroidRuntime(9154):在android.app.FragmentManagerImpl $ 1.run(FragmentManager.java:443) 11-04 17:46:27.141:E / AndroidRuntime(9154):在android.os.Handler.handleCallback(Handler.java:733) 11-04 17:46:27.141:E / AndroidRuntime(9154):在android.os.Handler.dispatchMessage(Handler.java:95) 11-04 17:46:27.141:E / AndroidRuntime(9154):在android.os.Looper.loop(Looper.java:136) 11-04 17:46:27.141:E / AndroidRuntime(9154):在android.app.ActivityThread.main(ActivityThread.java:5021) 11-04 17:46:27.141:E / AndroidRuntime(9154):at java.lang.reflect.Method.invokeNative(Native Method) 11-04 17:46:27.141:E / AndroidRuntime(9154):at java.lang.reflect.Method.invoke(Method.java:515) 11-04 17:46:27.141:E / AndroidRuntime(9154):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:827) 11-04 17:46:27.141:E / AndroidRuntime(9154):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) 11-04 17:46:27.141:E / AndroidRuntime(9154):at dalvik.system.NativeStart.main(Native Method)
这是我的数据库助手类:
public class LectureDatabaseHelper extends SQLiteOpenHelper {
public static final String MYDATABASE_NAME = "LECTURE_DATABASE";
public static final String MYDATABASE_TABLE = "LECTURE_TABLE";
public static final int MYDATABASE_VERSION = 2;
public static final String KEY_ID = "_id";
public static final String USERNAME = "USERNAME";
public static final String LECTURE_NAME = "LECTURE_NAME";
public static final String LECTURE_DATE = "LECTURE_DATE";
public static final String LECTURE_NOTES = "LECTURE_NOTES";
private static final String SCRIPT_CREATE_DATABASE =
"create table " + MYDATABASE_TABLE + " ("
+ KEY_ID + " integer primary key autoincrement, "
+ USERNAME + " text not null, "
+ LECTURE_NAME + " text not null, "
+ LECTURE_DATE + " text not null, "
+ LECTURE_NOTES + " text null);";
public LectureDatabaseHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SCRIPT_CREATE_DATABASE);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE "+MYDATABASE_TABLE+";");
onCreate(db);
}
}
答案 0 :(得分:0)
public String getuserid() {
// TODO Auto-generated method stub
// write this method in dbhelper class
SQLiteDatabase db = null;
String selectQuery = "SELECT KEY_ID,USERNAME,LECTURE_NAME,LECTURE_DATE,LECTURE_NOTES FROM yourtable";
StringBuilder sb= new StringBuilder();
try {
db = this.getWritableDatabase();
Cursor cursor1 = db.rawQuery(selectQuery, null);
while (cursor1.moveToNext()) {
String KEY_ID= cursor1.getString(cursor1.getColumnIndex("KEY_ID"));
sb.append(KEY_ID);
sb.append("-");
String USERNAME= cursor1.getString(cursor1.getColumnIndex("USERNAME"));
sb.append(USERNAME);
sb.append("-");
String LECTURE_NAME=cursor1.getString(cursor1.getColumnIndex("LECTURE_NAME"));
sb.append(LECTURE_NAME);
sb.append("-");
String LECTURE_NOTES = cursor1.getString(cursor1.getColumnIndex("LECTURE_NOTES "));
sb.append(LECTURE_NOTES );
}
} catch (Exception e) {
e.printStackTrace();
}
return sb;
}
----------
Call Above method for fetching Data from DBSqlite
String mtext=dbhelperobject.getuserid();
if(mtext!=null)
{
yourtextview.settext(mtext)
}
答案 1 :(得分:0)
我建议你做这样的事
public String queryNotes(int lectureID,String user){
String[] columns = new String[]{KEY_ID,USERNAME,LECTURE_NAME, LECTURE_DATE,LECTURE_NOTES};
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE,columns ,
KEY_ID+"=? AND "+USERNAME+"=?" , new String[] { String.valueOf(lectureID),String.valueOf(user) }, null, null, null,null);
StringBuilder sb=new StringBuilder();
while (cursor.moveToNext()) {
ab.append(cursor.getString(4)));
}
cursor.close();
return sb.toString();
}
答案 2 :(得分:0)
一旦您阅读了logcat,就很容易发现错误。
没有这样的列:LECTURE_NOTES(代码1):,编译时:SELECT _id, USERNAME,LECTURE_NAME,LECTURE_DATE,LECTURE_NOTES,来自LECTURE_TABLE WHERE _id =? AND USERNAME =?
您没有任何名为LECTURE_NOTES
的列。请务必通过查看CREATE TABLE
查询
如果您最近编辑过数据库,请确保卸载应用程序或更改数据库版本,以便强制创建新数据库