English 中文(简体)
无法从 sqlite 获取信息
原标题:cannot retrieve the information from sqlite

I have 3 buttons on my screen choose image, savedata, checkdata. When I click on check data it shows a blank screen, it is not showing any images There is a warning that The value of the field DataManipulator.insert is not used Can any one correct my code? This is my 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)
问题回答
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();

时 时

 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;
   }




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签