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)