本文介绍了SQLite数据库表重建的每个应用程序重启的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是新的Android开发。我在我的应用程序的问题。我的SQLite数据库表是重新创建每个系统重新启动,以及所有保存的内容从表中灰飞烟灭。任何人都可以请帮我解决这个问题?
这是我的数据库类....
包Nsh.android.sms;
进口android.content.ContentValues;
进口android.content.Context;
进口android.database.Cursor;
进口android.database.sqlite.SQLiteDatabase;
进口android.database.sqlite.SQLiteDatabase.CursorFactory;
进口android.database.sqlite.SQLiteOpenHelper;
进口android.provider.SyncStateContract.Helpers;
公共类数据库{
公共静态最后弦乐MYDATABASENAME =Mylanguagedbnew.db;
公共静态最后弦乐MYDATABASETABLE =Mylanguagedb_table1;
公共静态最终诠释MYDATABASEVERSION = 1;
公共静态最后弦乐KEY_id =_ ID;
公共静态最后弦乐KEY_language =语言;
公共静态最后弦乐KEY_description =languagedescription;
私有静态最后弦乐SCRIPT_CREATE_DATABASE =创建表,如果不存在
+ MYDATABASETABLE +(+ KEY_id +整数主键自动增量,+ KEY_language +
文字不为空,+ KEY_description +文字NOT NULL);;
私人datahelper sqliteopenhelper;
公共静态字符串userselectedlang;
私人SQLiteDatabase sqlitedatabase;
私人上下文的背景下;
公共数据库(上下文C){
的System.out.println(的DbContext !!! 1111);
上下文= C;
}
公共数据库openToread()抛出android.database.SQLException {
的System.out.println(DBINSERTTTTTTTTT !!! OPENREAD22222);
sqliteopenhelper =新datahelper(背景下,MYDATABASENAME,空,MYDATABASEVERSION);
的System.out.println(DBINSERTTTTTTTTT !!! OPENREAD22222);
sqlitedatabase = sqliteopenhelper.getReadableDatabase();
的System.out.println(DBINSERTTTTTTTTT !!! OPENREAD22222);
回到这一点;
}
公共数据库openTowrite()抛出android.database.SQLException {
的System.out.println(DBINSERTTTTTTTTT !!! OPENWRITE22222);
sqliteopenhelper =新datahelper(背景下,MYDATABASENAME,空,MYDATABASEVERSION);
sqlitedatabase = sqliteopenhelper.getWritableDatabase();
回到这一点;
}
公共无效的close(){
sqliteopenhelper.close();
}
公共光标retriveall(){
的System.out.println(获取所有OK !!!!!!!!!);
的String []列= {KEY_id,KEY_language,KEY_description};
光标光标= sqlitedatabase.query(MYDATABASETABLE,列,NULL,NULL,NULL,NULL,NULL);
返回游标;
}
公共光标retrivelanguages(){
的String []列= {KEY_language};
光标C = sqlitedatabase.query(MYDATABASETABLE,列,NULL,NULL,NULL,NULL,NULL);
返回℃;
}
公共光标retrieveselectedlanguagedecription(){
userselectedlang = listlang.selectedlanguage;
查询字符串=从Mylanguagedb_table1,其中语言选择languagedescription
='+ userselectedlang +';;
游标c1 = sqlitedatabase.rawQuery(查询,NULL);
返回C1;
}
众长刀片(String语言,字符串languagedescription){
的System.out.println(DBINSERTTTTTTTTT!INSERT !!!!!!!!! 1111);
ContentValues contentvalues =新ContentValues();
contentvalues.put(KEY_language,语言);
contentvalues.put(KEY_description,languagedescription);
返回sqlitedatabase.insert(MYDATABASETABLE,空,contentvalues);
}
公众诠释用deleteAll(){
返回sqlitedatabase.delete(MYDATABASETABLE,NULL,NULL);
}
/ *公众诠释deleterow(){
字符串selectedrow = listlang.selectedlanguage;
返回sqlitedatabase.delete(MYDATABASETABLE,selectedrow,NULL);
} * /
私有静态类datahelper扩展SQLiteOpenHelper {
公共datahelper(上下文的背景下,字符串名称,CursorFactory厂,
INT版){
超(背景下,名称,厂家,版本);
// TODO自动生成构造函数存根
}
@覆盖
公共无效的onCreate(SQLiteDatabase DB){
// TODO自动生成方法存根
db.execSQL(SCRIPT_CREATE_DATABASE);
}
@覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
// TODO自动生成方法存根
//db.update(table,价值观,whereClause,whereArgs)
}
}
}
解决方案
您可以通过的onCreate使用
方法,你可以限制数据的插入(如果是固定数据)只有当应用程序第一次executed.This将解决重复。共享preferences
解决这个问题()
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.splash);
共享preferences preF = getShared preferences(preFS_NAME1,MODE_PRIVATE);
布尔first1 = pref.getBoolean(preF_FIRST1,假);
如果(first1!=真)
{
getShared preferences(preFS_NAME1,MODE_PRIVATE)
。编辑()
.putBoolean(preF_FIRST1,真)
。承诺();
//此处插入数据,或使用后台任务来做到这一点
}
}
I am new to Android development. I have a problem in my application. My SQLite database tableis recreating on each system reboot, and all saved contents are wiped out from table. Can anyone please help me to solve this problem?
here is my Database class....
package Nsh.android.sms;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.SyncStateContract.Helpers;
public class Database {
public static final String MYDATABASENAME="Mylanguagedbnew.db";
public static final String MYDATABASETABLE="Mylanguagedb_table1";
public static final int MYDATABASEVERSION=1;
public static final String KEY_id="_id";
public static final String KEY_language="languages";
public static final String KEY_description="languagedescription";
private static final String SCRIPT_CREATE_DATABASE="create table if not exists
"+MYDATABASETABLE+" ("+KEY_id+" integer primary key autoincrement, "+KEY_language+"
text not null, "+KEY_description+ " text not null);";
private datahelper sqliteopenhelper;
public static String userselectedlang;
private SQLiteDatabase sqlitedatabase;
private Context context;
public Database(Context c){
System.out.println("DBcontext!!!1111");
context=c;
}
public Database openToread()throws android.database.SQLException{
System.out.println("DBINSERTTTTTTTTT!!!OPENREAD22222");
sqliteopenhelper=new datahelper(context,MYDATABASENAME,null,MYDATABASEVERSION);
System.out.println("DBINSERTTTTTTTTT!!!OPENREAD22222");
sqlitedatabase=sqliteopenhelper.getReadableDatabase();
System.out.println("DBINSERTTTTTTTTT!!!OPENREAD22222");
return this;
}
public Database openTowrite()throws android.database.SQLException{
System.out.println("DBINSERTTTTTTTTT!!!OPENWRITE22222");
sqliteopenhelper=new datahelper(context,MYDATABASENAME,null,MYDATABASEVERSION);
sqlitedatabase=sqliteopenhelper.getWritableDatabase();
return this;
}
public void close(){
sqliteopenhelper.close();
}
public Cursor retriveall(){
System.out.println("RETRIEVE ALL OK!!!!!!!!!");
String[] columns={KEY_id,KEY_language,KEY_description};
Cursor cursor=sqlitedatabase.query(MYDATABASETABLE, columns,null,null,null,null,null);
return cursor;
}
public Cursor retrivelanguages(){
String[] columns={KEY_language};
Cursor c=sqlitedatabase.query(MYDATABASETABLE, columns,null,null,null,null,null);
return c;
}
public Cursor retrieveselectedlanguagedecription(){
userselectedlang=listlang.selectedlanguage;
String query="select languagedescription from Mylanguagedb_table1 where languages
='"+userselectedlang+"';";
Cursor c1=sqlitedatabase.rawQuery(query,null);
return c1;
}
public long insert(String language,String languagedescription){
System.out.println("DBINSERTTTTTTTTT!!!INSERT !!!!!!!!!1111");
ContentValues contentvalues=new ContentValues();
contentvalues.put(KEY_language,language);
contentvalues.put(KEY_description,languagedescription);
return sqlitedatabase.insert(MYDATABASETABLE, null,contentvalues);
}
public int deleteall(){
return sqlitedatabase.delete(MYDATABASETABLE,null,null);
}
/*public int deleterow(){
String selectedrow=listlang.selectedlanguage;
return sqlitedatabase.delete(MYDATABASETABLE,selectedrow, null);
}*/
private static class datahelper extends SQLiteOpenHelper{
public datahelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(SCRIPT_CREATE_DATABASE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
//db.update(table, values, whereClause, whereArgs)
}
}
}
解决方案
You can solve this problem by using SharedPreferences
in onCreate()
method ,you can limit the insertion of the data (if it is fixed data) only when the app first time executed.This will solve the duplication.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
SharedPreferences pref = getSharedPreferences(PREFS_NAME1,MODE_PRIVATE);
boolean first1 = pref.getBoolean(PREF_FIRST1, false);
if(first1!=true)
{
getSharedPreferences(PREFS_NAME1,MODE_PRIVATE)
.edit()
.putBoolean(PREF_FIRST1, true)
.commit();
// insert data here or use Background Task to do that
}
}
这篇关于SQLite数据库表重建的每个应用程序重启的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!