SQLite 插入返回 -1 。删除包含数据库的文件夹后
Posted
技术标签:
【中文标题】SQLite 插入返回 -1 。删除包含数据库的文件夹后【英文标题】:SQLite insert return -1 . after Deleting Folder which contain Database 【发布时间】:2019-07-17 04:48:42 【问题描述】:在以编程方式删除包含数据库的文件夹后,当下次调用插入查询时,它返回-1。我认为解决方案是通过调用数据库构造函数再次创建数据库。但我不知道如何在片段中执行此操作。
数据库类:
public class SQliteHelperClass extends SQLiteOpenHelper
public static final String Dabase_name= "UserRecord.db";
public static final String Table_name= "RTable";
public static final String col_0= "Id";
public static final String col_1= "KM";
public static final String col_2= "MDATE";
public static final String col_3= "MTIME";
public static final String col_4= "MDIFFER";
public SQliteHelperClass(Context context)
super(context, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
+ File.separator+"MyCarData"
+ File.separator + Dabase_name, null, 1);
@Override
public void onCreate(SQLiteDatabase db)
db.execSQL("CREATE TABLE "+ Table_name +"(Id INTEGER PRIMARY KEY AUTOINCREMENT, KM INTEGER, MDATE TEXT, MTIME TEXT , MDIFFER INTEGER) ");
public boolean insertData(KmDataModel kmDataModel)
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(col_0,kmDataModel.getId());
contentValues.put(col_1,kmDataModel.getKm());
contentValues.put(col_2,kmDataModel.getDate());
contentValues.put(col_3,kmDataModel.getTime());
contentValues.put(col_4,kmDataModel.getDffKm());
long result =db.insert(Table_name,null,contentValues);
if(result == -1)
return false;
else
return true;
调用删除特定文件夹的包含的删除方法:
public void DeleteDataFolder()
File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
+ File.separator+"MyCarData"
+ File.separator);
if (dir.isDirectory())
String[] children = dir.list();
for (int i = 0; i < children.length; i++)
Log.e("File_List",children[i]);
new File(dir, children[i]).delete();
请注意,如果我关闭应用程序并再次启动它,那么数据库工作正常,所以我正在调用强制应用程序关闭但我不想关闭应用程序的方法。
【问题讨论】:
这是意料之中的。您正在删除数据库文件。如果执行失败,Insert 将返回 -1,这应该发生在这里,因为文件不存在。 我可以理解,但是在插入数据之前我该怎么做才能再次创建数据库 理想情况下,您不应该删除数据库文件,如果您真的想这样做,请确保在插入之前检查表是否存在。如果表不存在,则创建它,然后继续插入。 boolean tableExists(SQLiteDatabase db, final String tableName) Cursor cursor = null;尝试 cursor = db.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '" + tableName + "'", null); return (cursor != null && cursor.getCount() > 0); 最后 if (null != cursor) cursor.close(); 使用able sn -p 检查表是否存在 【参考方案1】:您认为需要重新创建/创建另一个数据库助手实例的想法是正确的。
所以刷新现有实例,例如myInstanceOfDBHelper = new SQliteHelperClass(context)
如果你不这样做,那么你会在日志中得到类似于以下内容的内容:-
2019-02-23 16:15:32.049 31038-31038/abc.so54838140sqlitealtfilelocation D/DBINSERT: Attempt # 1 at inserting into DB
2019-02-23 16:15:32.070 31038-31038/abc.so54838140sqlitealtfilelocation E/File_List: UserRecord.db
2019-02-23 16:15:32.070 31038-31038/abc.so54838140sqlitealtfilelocation E/File_List: UserRecord.db-wal
2019-02-23 16:15:32.071 31038-31038/abc.so54838140sqlitealtfilelocation E/File_List: UserRecord.db-shm
2019-02-23 16:15:32.071 31038-31038/abc.so54838140sqlitealtfilelocation D/DBINSERT: Attempt # 2 at inserting into DB
2019-02-23 16:15:32.074 31038-31038/abc.so54838140sqlitealtfilelocation E/SQLiteDatabase: Error inserting MDIFFER=30 KM=100 Id=1 MTIME=10:30 MDATE=2019-01-01
android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: RTable.Id (code 1555 SQLITE_CONSTRAINT_PRIMARYKEY)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:796)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1564)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1433)
at abc.so54838140sqlitealtfilelocation.SQliteHelperClass.insertData(SQliteHelperClass.java:48)
at abc.so54838140sqlitealtfilelocation.MainActivity.onCreate(MainActivity.java:30)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
您可能想知道删除后如何得到重复,可能是旧连接允许访问缓存数据
以下代码用于上述:-
您的 SQliteHelperClass(稍作修改以输出插入尝试#) KmDataModel 类的近似值,以及 调用活动(更易于编码)活动代码是:-
public class MainActivity extends AppCompatActivity
SQliteHelperClass mDBHlpr;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(Build.VERSION.SDK_INT >= 23)
ExternalStoragePermissions.verifyStoragePermissions(this);
mDBHlpr = new SQliteHelperClass(this); //<<<<<<<<<< original instantiation
KmDataModel k = new KmDataModel();
k.setId(1);
k.setKm(100);
k.setDate("2019-01-01");
k.setTime("10:30");
k.setDffKm(30);
mDBHlpr.insertData(k,1);
mDBHlpr.DeleteDataFolder();
// If not commeneted out line below will re-instantiate the DB helper
//mDBHlpr = new SQliteHelperClass(this); //<<<<<<<<<< COMMENTED OUT TO FAIL
mDBHlpr.insertData(k,2);
注释掉的行没有注释掉上面的作品。
= 所以您需要做的就是在您的片段中应用上述内容,这需要获得合适的上下文。如果您在获取上下文时遇到困难,那么 Using context in a fragment
可能会有所帮助。
通过重新实例化,运行后的日志是:-
2019-02-23 16:19:39.179 31176-31176/abc.so54838140sqlitealtfilelocation D/DBINSERT: Attempt # 1 at inserting into DB
2019-02-23 16:19:39.200 31176-31176/abc.so54838140sqlitealtfilelocation E/File_List: UserRecord.db
2019-02-23 16:19:39.200 31176-31176/abc.so54838140sqlitealtfilelocation E/File_List: UserRecord.db-wal
2019-02-23 16:19:39.200 31176-31176/abc.so54838140sqlitealtfilelocation E/File_List: UserRecord.db-shm
2019-02-23 16:19:39.202 31176-31176/abc.so54838140sqlitealtfilelocation D/DBINSERT: Attempt # 2 at inserting into DB
2019-02-23 16:19:39.239 31176-31176/abc.so54838140sqlitealtfilelocation D/OpenGLRenderer: Skia GL Pipeline
【讨论】:
以上是关于SQLite 插入返回 -1 。删除包含数据库的文件夹后的主要内容,如果未能解决你的问题,请参考以下文章