1. Purpose
When we strictly operate the database, we always encounter this situation, after the program restarts, the database tables have been established, do not need to be established again. At the beginning of the project, the use of the Android local database Sqlitedatabase time, also encountered this situation. To navigate through its methods, only findedittable may have this function, so the following experiment was done:
String TableName ="";Tablename=Msqlitedatabase.findedittable(TABLENAME);//This function hasBugLog.I("LN","result ="+ tableName);if(TableName = =NULL|| Tablename.equals ("")){Msqlitedatabase. Execsql ("Create TABLE"+TABLENAME+"(_id INTEGER PRIMARY KEY autoincrement,"+Column_username+"VARCHAR (") is not NULL, "+column_useraddress+"VARCHAR ()"));}Else{Log.I("LN","The table has been created.");}
Note: private static final TABLENAME = "user";
2. Questions
Multiple experiments can be done, and when the table user is not yet established, the Findedittable method is called, and the program returns "user" firmly. This results in the table not being established in the database, and subsequent database operations cannot be performed.
at:4559.124 3649-3649-ten: :59.125 3649-3649/com.ln.dbstorage i/ln:the table has been created.
3. Reason
I have a hunch that the reason may be in the source code inside, so open the source of Sqlitedatabase to see exactly. Please see the source code for the section on findedittable.
/*** Finds The name of the first table, which is editable. * * @paramTablesa list of tables * @returnThe first table listed */ Public StaticString findedittable (string tables) {if(! Textutils.isempty (tables)) {//Find the first word terminated by either a space or a comma intSpacepos = Tables.indexof ("');intCommapos = Tables.indexof (', ');if(Spacepos >0&& (Spacepos < Commapos | | Commapos <0)) {returnTables.substring (0, Spacepos); }else if(Commapos >0&& (Commapos < Spacepos | | Spacepos <0) ) {returnTables.substring (0, Commapos); }returnTables; }Else{throw NewIllegalStateException ("Invalid Tables"); } }
See here, the situation is at a glance.
4. The programme
Presumably the code friends know, SQLite ". Tables" command, you can find all the existing table names. I think there may be tables in the database that hold all the table information. Luckily, Sqlite_master appeared in my sight. Therefore, we have the following solutions.
// according to the indication obtained table msqlitedatabase. Rawquery ("select * from sqlite_master where tbl_name =?" string[]{TABLENAME}); Log. I ("LN",+ cursor.getcount ()) ;
The results of the operation are as follows:
to:52.711 30008-300080
5. Resolve
cursor cursor =Msqlitedatabase. Rawquery ("SELECT * from sqlite_master where tbl_name =?", Newstring[]{TABLENAME});Log.I("LN","Table num:"+ Cursor.getcount ()); if(Cursor.getcount ()! =1){Msqlitedatabase. Execsql ("Create TABLE"+TABLENAME+"(_id INTEGER PRIMARY KEY autoincrement,"+Column_username+"VARCHAR (") is not NULL, "+column_useraddress+"VARCHAR ()"));}Else{Log.I("LN","The table has been created.");}
--2016.5.10 in Zhengzhou
Improvements to the Sqlitedatabase.findedittable