我的应用程序将从服务器加载数据并将其保存到设备android上的数据库sqlite。我的问题是当我想在数据库中测试数据重复时 - >我的应用错误。你能帮我吗!代码上的位置“ * ”错误 CLASS DB_Adapter查询数据库
public class DB_Adapter {
public static final String INVOICEID = "invoiceid";
public static final String NUMBERPHONE = "numberphone";
public static final String ADDRESS = "address";
public static final String NUMBER = "number";
public static final String SNAME = "name";
public static final String URL = "url";
public static final String STATUS = "status";
public static final String DB_NAME = "CSDL";
public static final String DB_TABLE = "Saigonfleamarket";
public static final int DB_VERSION = 2;
private final Context mContext;
private SQLiteDatabase mDB;
private DBHelper mDBHelper;
public DB_Adapter(Context c) {
mContext = c;
}
private static class DBHelper extends SQLiteOpenHelper
{
public DBHelper(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
try
{
db.execSQL("CREATE TABLE Saigonfleamarket (number integer PRIMARY KEY autoincrement, invoiceid text,name text, numberphone text, address text,url text, status text);");
} catch (SQLException ex)
{
ex.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.i("DBAdapter", "Updating database...");
db.execSQL("DROP TABLE IF EXISTS Saigonfleamarket");
onCreate(db);
}
}
public DB_Adapter openDB()
{
mDBHelper = new DBHelper(mContext, DB_NAME, null, DB_VERSION);
mDB = mDBHelper.getWritableDatabase();
return this;
}
public void closeDB()
{
mDBHelper.close();
}
public long insert(String _invoiceid, String _name, String _numberphone, String _address,
String _url, String _status)
{
ContentValues cv = new ContentValues();
cv.put(INVOICEID, _invoiceid);
cv.put(SNAME, _name);
cv.put(NUMBERPHONE, _numberphone);
cv.put(ADDRESS, _address);
cv.put(URL, _url);
cv.put(STATUS, _status);
return mDB.insert(DB_TABLE, null, cv);
}
public boolean remove(int _number)
{
return mDB.delete(DB_TABLE, INVOICEID + "=" + _number, null) > 0;
}
public Cursor getAllStudent()
{
return mDB.query(DB_TABLE, new String[] {
NUMBER, INVOICEID, SNAME, NUMBERPHONE, ADDRESS, URL, STATUS
}, null, null, null, null, null);
}
public Cursor getStudentById(int _number)
{
Cursor mCursor = mDB.query(true, DB_TABLE, new String[] {
NUMBER, INVOICEID, SNAME, NUMBERPHONE, ADDRESS, URL, STATUS
}, NUMBER + "=" + _number, null, null, null, null, null);
if (mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;
}
***public Boolean test (String mahoadon)***
{
Boolean flag=true;
Cursor mCursor = mDB.query(true, DB_TABLE, new String[] {
NUMBER, INVOICEID, SNAME, NUMBERPHONE, ADDRESS, URL, STATUS
}, INVOICEID + "=" + mahoadon, null, null, null, null, null);
if (mCursor != null)
{
flag=false;
}
else
{
flag=true;
}
return flag;
}
}
CLASS LOAD DATA AND SAVE TO DATABASE
private DB_Adapter mDb = new DB_Adapter(this);
// class load data từ server về thiết bị
private class LoadData extends AsyncTask<Void, Void, Void> {
private ProgressDialog progressDialog;
private Boolean flag = false;
String phonenumber;
String name;
String address;
String url;
String code;
private JSONArray jArray;
private String result = null;
private InputStream is = null;
private StringBuilder sb = null;
private ArrayList<String> phonenumberArray = new ArrayList<String>();
private ArrayList<String> codeInvoiceArray = new ArrayList<String>();
private ArrayList<String> nameArray = new ArrayList<String>();
private ArrayList<String> addressArray = new ArrayList<String>();
private ArrayList<String> urlArray = new ArrayList<String>();
private ListView listview;
@Override
// can use UI thread here
protected void onPreExecute() {
this.progressDialog = ProgressDialog.show(Hoadon.this, "",
" Đang tải thông tin...");
}
// hiển thị thông tin lên listview của Activity
@Override
protected void onPostExecute(final Void unused) {
this.progressDialog.dismiss();
if (flag == true)
{
thongbao("Tải dữ liệu thành công");
}
else
{
thongbao("Hôm nay không có gì để giao!");
}
try {
listview = (ListView) findViewById(R.id.listView1);
this.progressDialog.dismiss();
listview.setAdapter(new DataAdapter(Hoadon.this, codeInvoiceArray
.toArray(new String[codeInvoiceArray.size()]), nameArray
.toArray(new String[nameArray.size()]),phonenumberArray
.toArray(new String[phonenumberArray.size()]), addressArray
.toArray(new String[addressArray.size()])));
listview.setOnItemClickListener(new OnItemClickListener() {
//xử lý sự kiện khi chọn item trên listview
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent j=new Intent(Hoadon.this,Menu_Main.class);
//lấy thông tin thông qua vị trí của item được chọn
String url = urlArray.get(position);
String address = addressArray.get(position);
String mahoadon = codeInvoiceArray.get(position);
String phone=phonenumberArray.get(position);
//chuyển thông tin này qua bên phần ký tên
j.putExtra("url", url);
j.putExtra("address", address);
j.putExtra("mahoadon", mahoadon);
j.putExtra("phone",phone);
j.putExtra("ID", IDinvoice);
startActivity(j);
}
});
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.toString(),
Toast.LENGTH_LONG).show();
}
}
// lấy thông tin từ server về dưới dạng json, được chuyển thành chuỗi và
// lưu vào các mảng
@Override
protected Void doInBackground(Void... params) {
try {
/*Calendar cal = Calendar.getInstance();
int date1 = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH) + 1;
int year = cal.get(Calendar.YEAR);
String str1 = date1 + "/" + month + "/" + year;*/
//Gửi thông tin id người giao hàng lên server để lấy danh sách hóa đơn về
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://longvansolution.tk/monthlytarget.php");
nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("user", IDinvoice));
/*nameValuePairs.add(new BasicNameValuePair("mydate", str1));*/
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
//chuyển thông tin nhận được về dạng chuỗi
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "iso-8859-1"), 80);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.toString(),
Toast.LENGTH_LONG).show();
}
//kiểm tra thông tin nhận được từ server
if (result.equalsIgnoreCase("null\n\n \n"))
{
flag = false;
//làm sạch mảng trước khi muốn thêm các thành phần để tránh tình trạng trùng lặp
phonenumberArray.clear();
codeInvoiceArray.clear();
nameArray.clear();
addressArray.clear();
urlArray.clear();
} else
{
flag = true;
try {
jArray = new JSONArray(result);
JSONObject json_data = null;
phonenumberArray.clear();
codeInvoiceArray.clear();
nameArray.clear();
addressArray.clear();
urlArray.clear();
mDb.openDB();
//sử dụng json lấy các giá trị thông qua các key
//gán nó vào các mảng tương ứng
for (int i = 0; i < jArray.length(); i++) {
json_data = jArray.getJSONObject(i);
phonenumber = json_data.getString("phonenumber");
address = json_data.getString("address");
name = json_data.getString("name");
url = json_data.getString("url");
code = json_data.getString("code");
phonenumberArray.add(phonenumber);
codeInvoiceArray.add(code);
nameArray.add(name);
addressArray.add(address);
urlArray.add(url);
***if(mDb.test(code)==true)
{
mDb.insert(code, name, phonenumber, address, url, "Chuagiao");
}***
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), e.toString(),
Toast.LENGTH_LONG).show();
} catch (ParseException e) {
Toast.makeText(getApplicationContext(), e.toString(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.toString(),
Toast.LENGTH_LONG).show();
}
}
mDb.closeDB();
return null;
}
}
LOG ERROR
12-12 11:08:31.979: W/dalvikvm(1384): threadid=14: thread exiting with uncaught exception (group=0x40015560)
12-12 11:08:31.987: E/AndroidRuntime(1384): FATAL EXCEPTION: AsyncTask #4
12-12 11:08:31.987: E/AndroidRuntime(1384): java.lang.RuntimeException: An error occured while executing doInBackground()
12-12 11:08:31.987: E/AndroidRuntime(1384): at android.os.AsyncTask$3.done(AsyncTask.java:200)
12-12 11:08:31.987: E/AndroidRuntime(1384): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
12-12 11:08:31.987: E/AndroidRuntime(1384): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
12-12 11:08:31.987: E/AndroidRuntime(1384): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
12-12 11:08:31.987: E/AndroidRuntime(1384): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
12-12 11:08:31.987: E/AndroidRuntime(1384): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
12-12 11:08:31.987: E/AndroidRuntime(1384): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
12-12 11:08:31.987: E/AndroidRuntime(1384): at java.lang.Thread.run(Thread.java:1019)
12-12 11:08:31.987: E/AndroidRuntime(1384): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
12-12 11:08:31.987: E/AndroidRuntime(1384): at android.os.Handler.<init>(Handler.java:121)
12-12 11:08:31.987: E/AndroidRuntime(1384): at android.widget.Toast.<init>(Toast.java:68)
12-12 11:08:31.987: E/AndroidRuntime(1384): at android.widget.Toast.makeText(Toast.java:231)
12-12 11:08:31.987: E/AndroidRuntime(1384): at com.example.finishdemo.Hoadon$LoadData.doInBackground(Hoadon.java:437)
12-12 11:08:31.987: E/AndroidRuntime(1384): at com.example.finishdemo.Hoadon$LoadData.doInBackground(Hoadon.java:1)
12-12 11:08:31.987: E/AndroidRuntime(1384): at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-12 11:08:31.987: E/AndroidRuntime(1384): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
12-12 11:08:31.987: E/AndroidRuntime(1384): ... 4 more
12-12 11:08:33.347: E/WindowManager(1384): Activity com.example.finishdemo.Hoadon has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405aa248 that was originally added here
12-12 11:08:33.347: E/WindowManager(1384): android.view.WindowLeaked: Activity com.example.finishdemo.Hoadon has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405aa248 that was originally added here
12-12 11:08:33.347: E/WindowManager(1384): at android.view.ViewRoot.<init>(ViewRoot.java:258)
12-12 11:08:33.347: E/WindowManager(1384): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
12-12 11:08:33.347: E/WindowManager(1384): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
12-12 11:08:33.347: E/WindowManager(1384): at android.view.Window$LocalWindowManager.addView(Window.java:424)
12-12 11:08:33.347: E/WindowManager(1384): at android.app.Dialog.show(Dialog.java:241)
12-12 11:08:33.347: E/WindowManager(1384): at android.app.ProgressDialog.show(ProgressDialog.java:107)
12-12 11:08:33.347: E/WindowManager(1384): at android.app.ProgressDialog.show(ProgressDialog.java:90)
12-12 11:08:33.347: E/WindowManager(1384): at android.app.ProgressDialog.show(ProgressDialog.java:85)
12-12 11:08:33.347: E/WindowManager(1384): at com.example.finishdemo.Hoadon$LoadData.onPreExecute(Hoadon.java:291)
12-12 11:08:33.347: E/WindowManager(1384): at android.os.AsyncTask.execute(AsyncTask.java:391)
12-12 11:08:33.347: E/WindowManager(1384): at com.example.finishdemo.Hoadon$4$1.run(Hoadon.java:246)
12-12 11:08:33.347: E/WindowManager(1384): at android.os.Handler.handleCallback(Handler.java:587)
12-12 11:08:33.347: E/WindowManager(1384): at android.os.Handler.dispatchMessage(Handler.java:92)
12-12 11:08:33.347: E/WindowManager(1384): at android.os.Looper.loop(Looper.java:130)
12-12 11:08:33.347: E/WindowManager(1384): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-12 11:08:33.347: E/WindowManager(1384): at java.lang.reflect.Method.invokeNative(Native Method)
12-12 11:08:33.347: E/WindowManager(1384): at java.lang.reflect.Method.invoke(Method.java:507)
12-12 11:08:33.347: E/WindowManager(1384): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-12 11:08:33.347: E/WindowManager(1384): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-12 11:08:33.347: E/WindowManager(1384): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
您无法在doInBackground()
中引用任何UI元素,在本例中为Toasts。您需要保存错误消息,可能在字符串中,然后在onPostExecute()
中显示。
仔细观察你的Toast,你试图在一个小Toast中显示非常长信息。通常,这些信息是用户保留的,因为它对他们没用。在最基本的层面上,设计师使用:
catch(Exception e) {
e.printStackTrace();
}
<强>加成强>
上述技术将阻止您的应用程序崩溃,但正如您所指出的那样,SQLite仍然会抛出异常。我注意到你没有正确使用String:
Cursor mCursor = mDB.query(true, DB_TABLE,
new String[] {NUMBER, INVOICEID, SNAME, NUMBERPHONE, ADDRESS, URL, STATUS},
INVOICEID + "=" + mahoadon, null, null, null, null, null);
正确的语法是:
INVOICEID + "='" + mahoadon + "'"
虽然最好使用selectionArgs
参数:
Cursor mCursor = mDB.query(true, DB_TABLE,
new String[] {NUMBER, INVOICEID, SNAME, NUMBERPHONE, ADDRESS, URL, STATUS},
INVOICEID + "=?", new String[] {mahoadon}, null, null, null, null);
这可以防止SQL注入和可能的缓存问题。