无法从sqlite中检索信息

时间:2012-05-23 05:44:56

标签: android sqlite

我的屏幕上有3个按钮,选择图像,保存数据,校验数据。当我点击检查数据时,它显示一个空白屏幕,它没有显示任何图像 有一个警告,表示不使用字段DataManipulator.insert的值 任何人都可以更正我的代码吗? 这是我的DataManipulator.java

public class DataManipulator {
    private static final  String DATABASE_NAME = "mydatabase.db";
    private static final int DATABASE_VERSION = 1;
    static final String TABLE_NAME = "newtable";
    private static Context context;
    static SQLiteDatabase db;
    private SQLiteStatement insert; 
    private static final String INSERT = " into "   + TABLE_NAME + " (name) values (?)";
    public DataManipulator(Context context) {
        DataManipulator.context = context;
        OpenHelper openHelper = new OpenHelper(DataManipulator.context);
        DataManipulator.db = openHelper.getWritableDatabase();
        this.insert = DataManipulator.db.compileStatement(INSERT);
    }
    public long insert(String name) {
        ContentValues args = new ContentValues();
        args.put("name", name);
        return db.insert(TABLE_NAME, null, args);
    }
    public void deleteAll() {
        db.delete(TABLE_NAME, null, null);
    }
    public List<String[]> selectAll()
    {
        List<String[]> list = new ArrayList<String[]>();
        Cursor cursor = db.query(TABLE_NAME, new String[] { "name"},  null, null, null, null, null); 
        int x=0;
        if (cursor.moveToFirst()) {
            do {
                String[] b1=new String[]{cursor.getString(0)};
                list.add(b1);
                x=x+1;
            } while (cursor.moveToNext());
        }
        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        } 
        cursor.close();
        return list;
    }
    public void delete(int rowId) {
        db.delete(TABLE_NAME, null, null); 
    }
    private static class OpenHelper extends SQLiteOpenHelper {
        OpenHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE " + TABLE_NAME + " (name TEXT)");
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
            onCreate(db);
        }
    }
}
我调试时

logcat输出

05-23 12:05:04.021: E/SQLiteDatabase(883): close() was never explicitly called on database '/data/data/list.certificates/databases/mydatabase.db' 
05-23 12:05:04.021: E/SQLiteDatabase(883): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
05-23 12:05:04.021: E/SQLiteDatabase(883):  at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1943)
05-23 12:05:04.021: E/SQLiteDatabase(883):  at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1007)
05-23 12:05:04.021: E/SQLiteDatabase(883):  at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:986)
05-23 12:05:04.021: E/SQLiteDatabase(883):  at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1051)
05-23 12:05:04.021: E/SQLiteDatabase(883):  at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:770)
05-23 12:05:04.021: E/SQLiteDatabase(883):  at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
05-23 12:05:04.021: E/SQLiteDatabase(883):  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
05-23 12:05:04.021: E/SQLiteDatabase(883):  at list.certificates.DataManipulator.<init>(DataManipulator.java:25)
05-23 12:05:04.021: E/SQLiteDatabase(883):  at list.certificates.SaveData.onClick(SaveData.java:57)
05-23 12:05:04.021: E/SQLiteDatabase(883):  at android.view.View.performClick(View.java:3480)
05-23 12:05:04.021: E/SQLiteDatabase(883):  at android.view.View$PerformClick.run(View.java:13983)
05-23 12:05:04.021: E/SQLiteDatabase(883):  at android.os.Handler.handleCallback(Handler.java:605)
05-23 12:05:04.021: E/SQLiteDatabase(883):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-23 12:05:04.021: E/SQLiteDatabase(883):  at android.os.Looper.loop(Looper.java:137)
05-23 12:05:04.021: E/SQLiteDatabase(883):  at android.app.ActivityThread.main(ActivityThread.java:4340)
05-23 12:05:04.021: E/SQLiteDatabase(883):  at java.lang.reflect.Method.invokeNative(Native Method)
05-23 12:05:04.021: E/SQLiteDatabase(883):  at java.lang.reflect.Method.invoke(Method.java:511)
05-23 12:05:04.021: E/SQLiteDatabase(883):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-23 12:05:04.021: E/SQLiteDatabase(883):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-23 12:05:04.021: E/SQLiteDatabase(883):  at dalvik.system.NativeStart.main(Native Method)
05-23 12:05:04.061: E/System(883): Uncaught exception thrown by finalizer
05-23 12:05:04.281: E/System(883): java.lang.IllegalStateException: Don't have database lock!
05-23 12:05:04.281: E/System(883):  at android.database.sqlite.SQLiteDatabase.verifyLockOwner(SQLiteDatabase.java:2090)
05-23 12:05:04.281: E/System(883):  at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2182)
05-23 12:05:04.281: E/System(883):  at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2178)
05-23 12:05:04.281: E/System(883):  at android.util.LruCache.trimToSize(LruCache.java:197)
05-23 12:05:04.281: E/System(883):  at android.util.LruCache.evictAll(LruCache.java:285)
05-23 12:05:04.281: E/System(883):  at android.database.sqlite.SQLiteDatabase.deallocCachedSqlStatements(SQLiteDatabase.java:2143)
05-23 12:05:04.281: E/System(883):  at android.database.sqlite.SQLiteDatabase.closeClosable(SQLiteDatabase.java:1126)
05-23 12:05:04.281: E/System(883):  at android.database.sqlite.SQLiteDatabase.finalize(SQLiteDatabase.java:1914)
05-23 12:05:04.281: E/System(883):  at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:182)
05-23 12:05:04.281: E/System(883):  at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:168)
05-23 12:05:04.281: E/System(883):  at java.lang.Thread.run(Thread.java:856)

2 个答案:

答案 0 :(得分:0)

check this one it may use
public DataHelper(Context context,String pagetype) {
  this.context = context;
  OpenHelper openHelper = new OpenHelper(this.context);
  this.db = openHelper.getWritableDatabase();
  if(pagetype.equals("settings")) {
  this.insertStmt = this.db.compileStatement(INSERT); 
  }

}    public long insert(String source,String destination,int sourceatcocode,int destinationatcocode,String folder){

   this.insertStmt.bindString(1, source);
   this.insertStmt.bindString(2, destination);
   this.insertStmt.bindLong(3, sourceatcocode);
   this.insertStmt.bindLong(4, destinationatcocode);
   this.insertStmt.bindString(5, folder);
  return this.insertStmt.executeInsert();

}

答案 1 :(得分:0)

 implement ur code in this code for retrieving
  public List<String> selectAll_trips() {
      List<String> list = new ArrayList<String>();

      Cursor cursor = this.db.query(TABLE_NAME, new String[] { "*" },
              null , null, null, null, "tripid desc");

      if (cursor.moveToFirst()) {
             do {
                 list.add(cursor.getString(0));
                 list.add(cursor.getString(1));
                 list.add(cursor.getString(2));
                 list.add(cursor.getString(3));
                 list.add(cursor.getString(4));
                 list.add(cursor.getString(5));

               } while (cursor.moveToNext());
          }
      if (cursor != null && !cursor.isClosed()) {
         cursor.close();
      }

      return list;
   }