2013-02-24 117 views
0

我有我的SQLite類的問題。事實上,我正在學習如何在Android上設置(更新和讀取)數據庫,並且我成功地在數據庫上編寫了數據庫,但是當我嘗試讀取信息並將它們顯示在屏幕上時,我的應用程序只是崩潰。
我搜索了這個問題,發現崩潰的原因是光標。我評論了遊標的方法,所以如果有人能幫助我,我會很感激。SQLite光標崩潰

這是我的數據庫類。

package com.example.sqlprogramming; 

    import android.content.ContentValues; 
    import android.content.Context; 
    import android.database.Cursor; 
    import android.database.SQLException; 
    import android.database.sqlite.SQLiteDatabase; 
    import android.database.sqlite.SQLiteOpenHelper; 

public class DatabaseClass { 
public static final String KEY_ROWID = "_id"; 
public static final String KEY_NAME = "person_name"; 
public static final String KEY_RATE = "person_rate"; 
private static final String DATABASE_NAME = "HotOrNotdb"; 
private static final String DATABASE_TABLE = "peopleTable"; 
private static final int DATABASE_VERSION = 2; 

private Dhelper ourHelperdb; 
private final Context ourContext; 
private SQLiteDatabase ourDatabase; 
private static class Dhelper extends SQLiteOpenHelper{ 

    public Dhelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 
     db.execSQL("create table if not exists " + DATABASE_TABLE + " (" + 
     KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
     KEY_NAME + "VARCHAR NOT NULL, " + 
     KEY_RATE + "VARCHAR NOT NULL);"); 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 
     db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE); 
     onCreate(db); 
    } 

} 
public DatabaseClass(Context c){ 
    ourContext = c; 
} 
public DatabaseClass open() throws SQLException{ 
    ourHelperdb = new Dhelper(ourContext); 
    ourDatabase = ourHelperdb.getWritableDatabase(); 
    return this; 
} 
public void close(){ 
    ourHelperdb.close(); 
} 
public long addEntry(String personName, String personHotness) { 
    // TODO Auto-generated method stub 
    ContentValues cv = new ContentValues(); 
    cv.put(KEY_NAME, personName); 
    cv.put(KEY_RATE, personHotness); 
    return ourDatabase.insert(DATABASE_TABLE, null, cv); 
} 
      //HERE IS MY PROBLEM WITH THE CURSOR 
public String getData() { 
    // TODO Auto-generated method stub 
    String[] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_RATE}; 
    String result = "hello"; 
    Cursor c = ourDatabase.query(true, DATABASE_TABLE, columns, null, null, null, null, null, null); 

    int iRowId = c.getColumnIndex(KEY_ROWID); 
    int iName = c.getColumnIndex(KEY_NAME); 
    int iRate = c.getColumnIndex(KEY_RATE); 

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){ 
      result = result + c.getString(iRowId) + " " + c.getString(iName) +" " + c.getString(iRate) + "\n"; 
    } 

    c.close(); 
    return result; 
} 
    } 

而這個類調用數據庫類,應該從數據庫中獲取信息以顯示在屏幕上。

package com.example.sqlprogramming; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 

public class SQLview extends Activity{ 
protected void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.layout_sqlview); 
    TextView textInfo = (TextView) findViewById (R.id.tvSQLinfo); 
    DatabaseClass info = new DatabaseClass(this); 
    info.open(); 
    String _data = info.getData(); 
    info.close(); 
    textInfo.setText(_data); 
} 
} 

再次感謝您。 我會盡快發佈stacktrace和logcat。

這裏是我的logcat /堆棧跟蹤

02-24 20:08:12.378: E/Curosr!(1221): I got an error here : 
02-24 20:08:12.378: E/Curosr!(1221): android.database.sqlite.SQLiteException: no such  column: person_name (code 1): , while compiling: SELECT _id AS _id, person_name, person_rate FROM peopleTable 
02-24 20:08:12.378: E/Curosr!(1221): at     android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
02-24 20:08:12.378: E/Curosr!(1221): at  android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882) 
02-24 20:08:12.378: E/Curosr!(1221): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493) 
02-24 20:08:12.378: E/Curosr!(1221): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 
02-24 20:08:12.378: E/Curosr!(1221): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58) 
02-24 20:08:12.378: E/Curosr!(1221): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37) 
02-24 20:08:12.378: E/Curosr!(1221): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44) 
02-24 20:08:12.378: E/Curosr!(1221): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314) 
02-24 20:08:12.378: E/Curosr!(1221): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161) 
02-24 20:08:12.378: E/Curosr!(1221): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032) 
02-24 20:08:12.378: E/Curosr!(1221): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200) 
02-24 20:08:12.378: E/Curosr!(1221): at com.example.sqlprogramming.DatabaseClass.getData(DatabaseClass.java:75) 
02-24 20:08:12.378: E/Curosr!(1221): at com.example.sqlprogramming.SQLview.onCreate(SQLview.java:14) 
02-24 20:08:12.378: E/Curosr!(1221): at android.app.Activity.performCreate(Activity.java:5104) 
02-24 20:08:12.378: E/Curosr!(1221): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
02-24 20:08:12.378: E/Curosr!(1221): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
02-24 20:08:12.378: E/Curosr!(1221): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
02-24 20:08:12.378: E/Curosr!(1221): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
02-24 20:08:12.378: E/Curosr!(1221): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
02-24 20:08:12.378: E/Curosr!(1221): at android.os.Handler.dispatchMessage(Handler.java:99) 
02-24 20:08:12.378: E/Curosr!(1221): at android.os.Looper.loop(Looper.java:137) 
02-24 20:08:12.378: E/Curosr!(1221): at android.app.ActivityThread.main(ActivityThread.java:5039) 
02-24 20:08:12.378: E/Curosr!(1221): at java.lang.reflect.Method.invokeNative(Native Method) 
02-24 20:08:12.378: E/Curosr!(1221): at java.lang.reflect.Method.invoke(Method.java:511) 
02-24 20:08:12.378: E/Curosr!(1221): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
02-24 20:08:12.378: E/Curosr!(1221): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
02-24 20:08:12.378: E/Curosr!(1221): at dalvik.system.NativeStart.main(Native Method) 
+0

始終與崩潰的錯誤發佈完整的堆棧跟蹤。 – 2013-02-24 19:37:09

+0

請在這裏添加logcat。 – Sajmon 2013-02-24 19:50:19

回答

0

你的create語句缺少幾個空格。嘗試使用:

db.execSQL("create table if not exists " + DATABASE_TABLE + " (" + 
    KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
    KEY_NAME + " VARCHAR NOT NULL, " + 
    KEY_RATE + " VARCHAR NOT NULL);"); 

根據您當前的聲明,您的最後兩個名成爲person_nameVARCHARperson_rateVARCHAR

+0

這就是我所嘗試的,但這不起作用,並且logcat消息是相同的,謝謝。 – R00t 2013-02-24 20:19:43

+0

@ R00t您必須先卸載並重新安裝應用程序,以使新的創建語句生效。 – 2013-02-24 20:21:10

+0

好的,謝謝,我現在就試試! – R00t 2013-02-24 20:23:16

0

如果你讀,你需要使用:

ourDatabase = ourHelperdb.getReadableDatabase(); 

當你打開你的數據庫,你設置它來寫。

(這是我的第一個猜測,後堆棧跟蹤)。

這裏的工作的例子,從我的一些代碼如何閱讀光標:

mDb = mDbHelper.getReadableDatabase(); 

Cursor c = mDb.query("Timesheets", new String[] { 
     KEY_TIMESHEETS_ROWID + " AS _id", KEY_TIMESHEETS_DESCRIPTION, 
     KEY_TIMESHEETS_TITLE, KEY_TIMESHEETS_DATE_START, 
     KEY_TIMESHEETS_DATE_END, KEY_TIMESHEETS_INVOICED, 
     KEY_TIMESHEETS_PROJECTID }, "timesheet_id = ?", 
     new String[] { String.valueOf(timesheetId) }, null, null, null); 

c.moveToFirst(); 

SimpleDateFormat dateFormat = new SimpleDateFormat(
     "yyyy-MM-dd HH:mm:ss"); 

Timesheet timesheet = new Timesheet(); 

while (c.isAfterLast() == false) { 

    timesheet.setTimesheetId(c.getInt(0)); 

    // ... 

    c.moveToNext(); 
} 

c.close(); 
+0

感謝您的評論,我添加了getReadableDatabase();方法到DBHelper並使用光標作爲示例,但這並不起作用,當我試圖讀取數據時它仍然崩潰。 – R00t 2013-02-24 19:53:18

+0

也可以讀取返回的'getWritableDatabase()'數據庫。使用'getReadableDatabase()'不是必需的。 – 2013-02-24 20:16:58