Android 2.3.3
我是使用AsyncTask的新手,所以,我的实现可能也可能不对。我必须使用AsyncTask将数据插入数据库。
问题是没有错误/异常,但是没有创建数据库。我不知道问题是与数据库还是asynctask有关。所以我提供下面的代码。请看看。
数据库文件:::
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class Database_SpeedDial extends SQLiteOpenHelper{
static String name = "speeddial_contacts";
static int version = 1;
SQLiteDatabase db;
public Database_SpeedDial(Context context) {
super(context, name, null, version);
// TODO Auto-generated constructor stub
db = getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE IF NOT EXISTS contacts(count INTEGER, image_url TEXT, name TEXT, number TEXT, bool INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public void insert_row(int count, String image_url, String name, String number, int bool) {
// TODO Auto-generated method stub
db.execSQL("INSERT INTO contacts VALUES('"+ count +"', '"+ image_url +"', '"+ name +"', '"+ number +"', '"+ bool +"')");
}
}
这是我的java代码:::
public class Class_Add_Contact extends Activity implements OnClickListener {
Database_SpeedDial dbObj;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_addcontact_add:
System.out.println("Inside onClick");
// Using AsyncTask
// alImage, alContactName ,.... are ArrayList<String>
InsertData id = new InsertData(alImage, alContactName, alPhoneNumber, alBoolean);
id.execute();
System.out.println("Finished Inserting");
break;
case R.id.btn_addcontact_cancel:
System.out.println("Cancel button clicked");
break;
default:
break;
}
}
private class InsertData extends AsyncTask<Void, Void, Void>{
ArrayList<String> alImage_insertdata;
ArrayList<String> alContactName_insertdata;
ArrayList<String> alPhoneNumber_insertdata;
ArrayList<Boolean> alBoolean_insertdata;
public InsertData(ArrayList<String> alImage, ArrayList<String> alContactName,
ArrayList<String> alPhoneNumber, ArrayList<Boolean> alBoolean)
{
alImage_insertdata = alImage;
alContactName_insertdata = alContactName;
alPhoneNumber_insertdata = alPhoneNumber;
alBoolean_insertdata = alBoolean;
}
@Override
protected void onPreExecute() {
System.out.println("Inside onPreExecute");
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
System.out.println("Inside doInBackground");
//dbObj = new Database_SpeedDial(this) -> I used this in onCreate()
// of the main class but didn't work, so shifted it here...
// This doesn't matter i guess...
dbObj = new Database_SpeedDial(Class_Add_Contact.this);
for(int i=0; i< alBoolean_insertdata.size();i++)
{
if(alBoolean.get(i))
{
dbObj.insert_row(i+1, alImage_insertdata.get(i).toString(),
alContactName_insertdata.get(i).toString(),
alPhoneNumber_insertdata.get(i).toString(),
1);
}
else
{
dbObj.insert_row(i+1, alImage_insertdata.get(i).toString(),
alContactName_insertdata.get(i).toString(),
alPhoneNumber_insertdata.get(i).toString(),
0);
}
}
return null;
}
@Override
protected void onPostExecute(Void res) {
System.out.println("Inside onPostExecute");
}
}
}
来自DDMS :::
的图片
代码执行没有任何问题,但是没有创建数据库。可能是什么问题呢?
答案 0 :(得分:3)
您无法将列命名为“count”。这是一个保留的关键字。
答案 1 :(得分:0)
在数据库上的第一个事务之后,将触发CREATE TABLE语句的onCreate()方法。因此,首先运行insert语句,然后检查是否已创建数据库。