Home > Mobile >  For loop in the update/insert sqlite database error
For loop in the update/insert sqlite database error

Time:10-09


In a for loop to perform database update/insert, database code below
C=
 Cursor dbhelper. GetReadableDatabase (.) rawQuery (
"Select * from ReadCount where domain=?" , new String [] {model. GetDomain ()});
Be sad etCount chtistina georgina rossetti.british poetess int count=();
Arthur c. lose ();
ContentValues CV=new ContentValues ();
CV. Put (" domain ", the model getDomain ());
CV. Put (" time ", the model getTime ());
CV. Put (" rulecount, "model. GetRulecount ());
If (count & gt; 0 {
The String where="domain=?" ;
String [] whereValues={model. GetDomain ()};
Dbhelper. GetWritableDatabase (), update (" ReadCount ", CV, the where, whereValues);
} else {
Dbhelper. GetWritableDatabase (), insert (" ReadCount ", null, CV);
}


By the collapse of baidu statistical background statistics will see a lot of android. Database. Sqlite. SQLiteReadOnlyDatabaseException: attempt to write a readonly database sqlite code (1032), (OS error - 2: No to the file or directory) error led to the collapse of but has not had this problem in my mobile phone, this kind of situation?

CodePudding user response:

Dbhelper sent to look at

CodePudding user response:

reference m - 1/f, oj response:
dbhelper to see


 public class ReadDbHelper extends SQLiteOpenHelper {
Public static final String CREATE_ReadCount="create TABLE IF NOT EXISTS ReadCount (id INTEGER PRIMARY KEY autoincrement, domain varchar, rulecount INTEGER, count INTEGER, time INTEGER)";
Public ReadDbHelper (Context Context, String name, CursorFactory factory,
{int version)
Super (context, "ReadCount", null, version);
}

@ Override
Public void onCreate (SQLiteDatabase db) {
Db. ExecSQL (CREATE_ReadCount);
System. The out. Println (" perform create s ");
}

@ Override
Public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
Db. ExecSQL (" DROP TABLE IF the EXISTS ReadCount ");
OnCreate (db);
}
}

CodePudding user response:

reference m - 1/f, oj response:
dbhelper to see


 
Private Context Context;
Private static ReadDbHelper dbhelper;
//the original
Public ReadDbCmd Context (Context) {
Enclosing context=context;
//dbhelper=new ReadDbHelper (context, null, null, 9);
Dbhelper=getInstance (context);
}

Private static ReadDbHelper getInstance Context (Context) {
If (dbhelper==null) {
Dbhelper=new ReadDbHelper (context, null, null, 9);
}
Return dbhelper;
}

CodePudding user response:

Attempt to write a readonly database
Android Sqlite, speaking, reading and writing are the two mirror, mirror will make a mistake,

Batch operation with a transaction and see

CodePudding user response:

reference 4 floor assky124 response:
attempt to write a readonly database
Android Sqlite, speaking, reading and writing are the two mirror, mirror will make a mistake,

Batch operation with a transaction about?


Use is getWritableDatabase, mobile phone normal in most people, some abnormal, the question is who also don't know, what mobile phone, what circumstance is not normal, only through baidu statistics to see this error

CodePudding user response:

reference 4 floor assky124 response:
attempt to write a readonly database
Android Sqlite, speaking, reading and writing are the two mirror, mirror will make a mistake,

Batch operation with a transaction about?


Or complains, transaction code below
 
Public void TransactionUpdrule (List The list) {
List Insertlist=new ArrayList (a);
List Updlist=new ArrayList (a);
for(int i=0; ICursor c=dbhelper. GetReadableDatabase (.) rawQuery (
"Select * from ReadCount where domain=?" , new String [] {list. Get (I). GetDomain ()});
Be sad etCount chtistina georgina rossetti.british poetess int count=();
Arthur c. lose ();
ContentValues CV=new ContentValues ();
CV. Put (" domain ", the list. The get (I). GetDomain ());
CV. Put (" time ", the list. The get (I). The getTime ());
CV. Put (" rulecount ", a list. The get (I). GetRulecount ());
If (count> 0 {
Updlist. Add (CV);
} else {
Insertlist. Add (CV);
}
}
SQLiteDatabase db=dbhelper. GetWritableDatabase ();
The beginTransaction ();//manually start the transaction
For (ContentValues v: insertlist) {
The insert (" ReadCount ", null, v);
}
For (ContentValues v: updlist) {
String [] whereValues={v.g etAsString (" domain ")};
The update (" ReadCount ", v, "domain=?" , whereValues);
}
Db. SetTransactionSuccessful ();//set the transaction success, not installed automatically rolled back not submit
Db. EndTransaction ();//processing complete
The close ();
}

  • Related