gpt4 book ai didi

android - 如何检查sqlite数据库中的表是否存在,如果不存在则创建一个并插入数据

转载 作者:搜寻专家 更新时间:2023-10-30 22:18:10 24 4
gpt4 key购买 nike

我现在正在编写一个需要在数据库中准备一些数据的应用程序,我尝试了这个问题中的方法

Ship an application with a database

事实证明你不能在 android 2.3 中复制数据库。

所以我设置了启动画面并想在那里运行一些数据初始化。

我尝试了 Thread 和 Asyctask,但仍然遇到这个奇怪的问题,我的 insert_data 方法不断出现异常

这让我很生气,请帮助

飞溅.java

public class Splash extends Activity {

private ChannelDB mDB;
private TextView loading;
private String channelS_TABLE;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
new SetDB().execute();

}

private class SetDB extends AsyncTask<String, String, String> {

@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
try {
Log.d("Splash","table check not running yet");
if (tabIsExist(channelS_TABLE) != true ){
Log.d("Splash","table check complete and not exist");
mDB.Reset();
Log.d("Splash","database reset");
some data insert
));
}else{
synchronized(this){
Log.i("Splash", "the table is there");
wait(3000);}
Intent i = new Intent();
i.setClassName("com.appkon.hdtvs",
"com.appkon.hdtvs.HDtvs");
finish();
startActivity(i);
}
}catch (Exception e) {

Log.i("Splash", "setDB exception");
Intent i = new Intent();
i.setClassName("com.appkon.hdtvs",
"com.appkon.hdtvs.HDtvs");
finish();
startActivity(i);
}
return null;
}


protected void onPreExecute(String load) {

}

protected void onPostExecute(String finish) {
Intent i = new Intent();
i.setClassName("com.appkon.hdtvs",
"com.appkon.hdtvs.HDtvs");
finish();
startActivity(i);
}
}


public boolean tabIsExist(String tableName){
boolean result = false;
if(tableName == null){
return false;
}
Cursor cursor= ChannelDB.check();
startManagingCursor(cursor);
try {
if(cursor.moveToNext()){
int count = cursor.getInt(0);
if(count>0){
result = true;
}
}

} catch (Exception e) {
Log.e(this.toString(),"error:"+e.toString());
Intent intent = new Intent(this,HDtvs.class);
startActivity(intent);
this.finish();
Log.d("Splash","tabIsExist Exception");
}
return result;
}


}

错误轨迹

12-05 07:52:20.542: INFO/System.out(575): debugger has settled (1460)
12-05 07:52:21.442: DEBUG/dalvikvm(575): GC freed 679 objects / 53552 bytes in 135ms
12-05 07:52:21.932: DEBUG/Splash(575): table check not running yet
12-05 07:52:21.932: DEBUG/Splash(575): table check complete and not exist
12-05 07:52:21.932: INFO/Splash(575): setDB exception
12-05 07:52:21.982: INFO/ActivityManager(51): Starting activity: Intent { cmp=com.appkon.hdtvs/.HDtvs }
12-05 07:52:22.482: WARN/ActivityManager(51): Duplicate finish request for HistoryRecord{44d4aa10 com.appkon.hdtvs/.Splash}
12-05 07:52:22.502: INFO/ActivityManager(51): Starting activity: Intent { cmp=com.appkon.hdtvs/.HDtvs }

感谢 Bindal 的出色解决方案,但我仍然有问题,我定义了一个方法来在我的数据库助手类中添加条目并使用它向数据库中插入数据,但它似乎不起作用

这是我在databasehelper类中定义的方法

 public void createchannelEntry(ChannelPoster channel) {
openDB();
ByteArrayOutputStream out = new ByteArrayOutputStream();
channel.getPoster().compress(Bitmap.CompressFormat.PNG, 100, out);
ContentValues cv = new ContentValues();
cv.put(KEY_POSTER, out.toByteArray());
cv.put(KEY_CHANNEL, channel.getChannel());
cv.put(KEY_PATH, channel.getPath());
cv.put(KEY_DBLINK, channel.getDBlink());

mDb.insert(channelS_TABLE, null, cv);
closeDB();
}

这是我插入数据的方式

Bitmap sherlock = BitmapFactory.decodeResource(getResources(), R.drawable.sherlock);

mDB.createchannelEntry(new ChannelPoster(aa, "aa" ,"ll" ,"ha" ));

我有一个 JavaBean 用于保存条目

public class ChannelPoster {
private Bitmap poster;
private String channel;
private String path;
private String dblink;

public ChannelPoster(Bitmap pi, String c, String p, String d) {
poster = pi;
channel = c;
path = p;
dblink = d;
}

public Bitmap getPoster() { return poster; }
public String getChannel() { return channel; }
public String getPath() { return path; }
public String getDBlink() { return dblink; }
}

现在我在这里添加Logcat记录

这是我的代码

 public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
try {
createDataBase();
} catch (Exception e) {
Intent intent = new Intent(this,HDtvs.class);
startActivity(intent);
this.finish();
Log.d("Splash","createDataBaseException");
}
}

public void createDataBase() throws Exception
{
boolean dbExist = checkDataBase();
if (dbExist){
Intent intent = new Intent(this,HDtvs.class);
startActivity(intent);
this.finish();
Log.d("Splash","table exist");
}
else
{
try{
mDB.Reset();
Log.d("Splash","database reset");
Bitmap bigbang1 = BitmapFactory.decodeResource(getResources(), R.drawable.bigbang1);


mDB.createchannelEntry(new ChannelPoster(bigbang1, "生活大爆炸(第一季)" ,"http://appkon.com/hdtvs/channel/bigbang1.xml" ,"http://movie.douban.com/subject/5372374/" ));

}catch (Exception e){
Log.d("splash","data insert exception");
Intent intent = new Intent(this,HDtvs.class);
startActivity(intent);
this.finish();
}
}
}

/**
* Check if the database already exist to avoid re-copying the file each
* time you open the application.
*
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase()
{
SQLiteDatabase checkDB = null;
try
{
String myPath = "data/data/com.appkon.hdtvs/databases/" + "channelDB";
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}catch (Exception e){
Log.d("Splash","database does't exist yet.");
}

if (checkDB != null){
checkDB.close();

return true;
}
else
return false;
}
}

这是我的logcat记录

> 12-05 08:35:14.222: INFO/System.out(824): debugger has settled (1435)
> 12-05 08:35:15.032: DEBUG/dalvikvm(824): GC freed 621 objects / 51304
> bytes in 98ms 12-05 08:35:15.373: ERROR/Database(824):
> sqlite3_open_v2("data/data/com.appkon.hdtvs/databases/channelDB",
> &handle, 2, NULL) failed 12-05 08:35:15.382: DEBUG/Splash(824):
> database does't exist yet. 12-05 08:35:15.382: DEBUG/splash(824): data
> insert exception 12-05 08:35:15.402: INFO/ActivityManager(51):
> Starting activity: Intent { cmp=com.appkon.hdtvs/.HDtvs } 12-05
> 08:35:16.753: INFO/ActivityManager(51): Displayed activity
> com.appkon.hdtvs/.HDtvs: 1298 ms (total 6152 ms)

最佳答案

public void createDataBase() throws Exception 
{
boolean dbExist = checkDataBase();
if (dbExist){
System.out.println("Database Exist");
}
else
{
this.getReadableDatabase();
try{

}catch (Exception e){
throw new Error(e.getMessage());
}
}
}

/**
* Check if the database already exist to avoid re-copying the file each
* time you open the application.
*
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase()
{
SQLiteDatabase checkDB = null;
try
{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}catch (Exception e){
System.out.println("database does't exist yet.");
}

if (checkDB != null){
checkDB.close();
System.out.println("My db is:- " + checkDB.isOpen());
return true;
}
else
return false;
}

关于android - 如何检查sqlite数据库中的表是否存在,如果不存在则创建一个并插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8382408/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com