Cursor或ContentValues无法读取我的SQLite数据? :d

时间:2012-03-19 08:18:15

标签: android sqlite cursor

我在关注thenewboston Android tut的

时遇到了问题

所以第一个问题是我确信我已经完全遵循了他所做的事情,但这次我的应用程序无效。

很抱歉,如果我的帖子太长,如果你能让我的问题清楚,你可以编辑它

所以这是我扩展了Activity的SQLiteExample类:

public class SQLiteExample extends Activity implements OnClickListener{
Button sqlUpdate, sqlView;
EditText sqlName, sqlHotness;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sqlliteexample);

    sqlUpdate = (Button) findViewById(R.id.bSqlUpdate);
    sqlView = (Button) findViewById(R.id.bSqlopenView);
    sqlName = (EditText) findViewById(R.id.etSqlName);
    sqlHotness = (EditText) findViewById(R.id.etSqlHot);

    sqlUpdate.setOnClickListener(this);
    sqlView.setOnClickListener(this);
}

public void onClick(View arg0) {
    // TODO Auto-generated method stub
    switch(arg0.getId()){
    case R.id.bSqlUpdate :

        boolean didItWork = true;

        try {
            String name = sqlName.getText().toString();
            String hotness = sqlHotness.getText().toString();

            HotOrNot entry = new HotOrNot(SQLiteExample.this);
            entry.open();
            entry.createEntry(name, hotness);
            entry.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            didItWork = false;
            String error = e.toString();
            Dialog d = new Dialog(this);
            d.setTitle("Dang It!");
            TextView tv = new TextView(this);
            tv.setText(error);
            d.setContentView(tv);
            d.show();
        }finally{
            if(didItWork){
                Dialog d = new Dialog(this);
                d.setTitle("Heck Yea!");
                TextView tv = new TextView(this);
                tv.setText("Success");
                d.setContentView(tv);
                d.show();
            }
        }
        break;
    case R.id.bSqlopenView :
        Intent i = new Intent("com.thenewboston.tama.SQLView");
        startActivity(i);
        break;
    }
}

我将setOnClickListener()设置为这里的2个按钮。

这是我的HotOrNot类,它包含了SQLite的东西:

public class HotOrNot {

public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "persons_name";
public static final String KEY_HOTNESS = "persons_hotness";

private static final String DATABASE_NAME = "HotOrNotdb";
private static final String DATABASE_TABLE = "peopleTable";
private static final int DATABASE_VERSION = 1;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

private static class DbHelper extends SQLiteOpenHelper {    

    public DbHelper(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 " + DATABASE_TABLE + " (" +
                KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_NAME + " TEXT NOT NULL, " + 
                KEY_HOTNESS + "TEXT 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 HotOrNot(Context c){
    ourContext = c;
}
//?????????????????????
public HotOrNot open() throws SQLException{
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}

public void close(){
    ourHelper.close();
}

public long createEntry(String name, String hotness) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(KEY_NAME, name);
    cv.put(KEY_HOTNESS, hotness);
    return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getData() {
    // TODO Auto-generated method stub
    String[] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_HOTNESS};
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
    String result = "";

    int iRow = c.getColumnIndex(KEY_ROWID);
    int iName = c.getColumnIndex(KEY_NAME);
    int iHotness = c.getColumnIndex(KEY_HOTNESS);

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

    return result;
}

这是我的SQLView扩展活动(对于第二个按钮):

public class SQLView extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sqlview);

    TextView tv = (TextView) findViewById(R.id.tvSQLinfo);
    HotOrNot info = new HotOrNot(this);
    info.open();
    String data = info.getData();
    info.close();

    tv.setText(data);
}

这是我点击SQLiteExample类中的R.id.bSqlopenView按钮时出现的错误:

03-19 14:47:54.504: I/Database(335): sqlite returned: error code = 1, msg = table peopleTable has no column named persons_hotness

在第一次

之后又出现了很多错误

已编辑:这是我的完整版本错误列表:D

    03-19 14:47:28.194: D/dalvikvm(335): GC_EXTERNAL_ALLOC freed 50K, 53% free 2554K/5379K, external 1625K/2137K, paused 107ms
03-19 14:47:49.634: W/KeyCharacterMap(335): No keyboard for id 0
03-19 14:47:49.634: W/KeyCharacterMap(335): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
03-19 14:47:54.504: I/Database(335): sqlite returned: error code = 1, msg = table peopleTable has no column named persons_hotness
03-19 14:47:54.515: E/Database(335): Error inserting persons_hotness= persons_name=
03-19 14:47:54.515: E/Database(335): android.database.sqlite.SQLiteException: table peopleTable has no column named persons_hotness: , while compiling: INSERT INTO peopleTable(persons_hotness, persons_name) VALUES(?, ?);
03-19 14:47:54.515: E/Database(335):    at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
03-19 14:47:54.515: E/Database(335):    at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
03-19 14:47:54.515: E/Database(335):    at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
03-19 14:47:54.515: E/Database(335):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
03-19 14:47:54.515: E/Database(335):    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41)
03-19 14:47:54.515: E/Database(335):    at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1149)
03-19 14:47:54.515: E/Database(335):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1569)
03-19 14:47:54.515: E/Database(335):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1426)
03-19 14:47:54.515: E/Database(335):    at com.thenewboston.tama.HotOrNot.createEntry(HotOrNot.java:70)
03-19 14:47:54.515: E/Database(335):    at com.thenewboston.tama.SQLiteExample.onClick(SQLiteExample.java:46)
03-19 14:47:54.515: E/Database(335):    at android.view.View.performClick(View.java:2485)
03-19 14:47:54.515: E/Database(335):    at android.view.View$PerformClick.run(View.java:9080)
03-19 14:47:54.515: E/Database(335):    at android.os.Handler.handleCallback(Handler.java:587)
03-19 14:47:54.515: E/Database(335):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-19 14:47:54.515: E/Database(335):    at android.os.Looper.loop(Looper.java:123)
03-19 14:47:54.515: E/Database(335):    at android.app.ActivityThread.main(ActivityThread.java:3683)
03-19 14:47:54.515: E/Database(335):    at java.lang.reflect.Method.invokeNative(Native Method)
03-19 14:47:54.515: E/Database(335):    at java.lang.reflect.Method.invoke(Method.java:507)
03-19 14:47:54.515: E/Database(335):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-19 14:47:54.515: E/Database(335):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-19 14:47:54.515: E/Database(335):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

更新您的数据库版本号,它应该删除并重新创建您的数据库以修复任何缺少的列:

private static final int DATABASE_VERSION = 2;  // previously 1

答案 1 :(得分:1)

KEY_HOTNESS之后的空格中放置您创建数据库的String,而不是:

//...
KEY_HOTNESS + "TEXT NOT NULL); "

写:

   //...
   KEY_HOTNESS + " TEXT NOT NULL); "