[英]FATAL EXCEPTION: AsyncTask #1 java.lang.RuntimeException: An error occured while executing doInBackground(), while sending an image to the Server
[英]FATAL EXCEPTION: AsyncTask #4 …java.lang.RuntimeException: An error occured while executing doInBackground()
我的代碼中有什么問題? 我可以編譯,但是當我運行該部件將數據保存到數據庫中時。 它崩潰了。 請幫忙 。
引起:android.database.sqlite.SQLiteException:靠近“tableproduct_table”:語法錯誤(代碼1):,同時編譯:create tableproduct_table(供應商文本,產品文本,pprice整數,cprice intege t android.database.sqlite.SQLiteConnection。位於android.database.sqlite.SQLiteConnection.prepare的android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)上android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)的nativePrepareStatement(Native Method)( SQLiteSession.java:588)
package kianyang.scbg;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;enter code here
import android.os.AsyncTask;
import android.widget.Toast;
/**
* Created by ABC on 1/12/2016.
*/
public class BackgroundTask extends AsyncTask<String,Void,String>{
Context ctx;
BackgroundTask(Context ctx)
{
this.ctx=ctx;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String method=params[0];
DbOperation dbOperation=new DbOperation(ctx);
if(method.equals("add_info"))
{
String Vendor=params[1];
String Product=params[2];
int Pprice=Integer.parseInt(params[3]);
int Cprice=Integer.parseInt(params[4]);
SQLiteDatabase db=dbOperation.getWritableDatabase();
dbOperation.addInformation(db,Vendor,Product,Pprice,Cprice);
return "One Row Inserted...";
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(ctx,result, Toast.LENGTH_LONG).show();
}
}
package kianyang.scbg;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/**
* Created by ABC on 1/12/2016.
*/
public class DbOperation extends SQLiteOpenHelper{
private static final int DB_VERSION= 1;
private static final String DB_NAME="product_info.db";
private static final String CREATE_QUERY="create table"+ProductContract.ProductEntry.TABLE_NAME+
"("+ ProductContract.ProductEntry.VENDOR+ " text,"+ ProductContract.ProductEntry.PRODUCT+ " text,"+
ProductContract.ProductEntry.PPRICE+ " integer,"+ ProductContract.ProductEntry.CPRICE+ " integer);";
DbOperation(Context ctx)
{
super(ctx,DB_NAME,null,DB_VERSION);
Log.d("Database operation","Database created...");
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_QUERY);
Log.d("Database operation", "Table created...");
}
public void addInformation(SQLiteDatabase db,String vendor,String product,int pprice,int cprice)
{
ContentValues contentValues=new ContentValues();
contentValues.put(ProductContract.ProductEntry.VENDOR,vendor);
contentValues.put(ProductContract.ProductEntry.PRODUCT,product);
contentValues.put(ProductContract.ProductEntry.PPRICE,pprice);
contentValues.put(ProductContract.ProductEntry.CPRICE,cprice);
db.insert(ProductContract.ProductEntry.TABLE_NAME, null, contentValues);
Log.d("Database operation", "One Row Inserted...");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
}
}
引起:android.database.sqlite.SQLiteException:靠近“tableproduct_table”:語法錯誤(代碼1):,同時編譯:create tableproduct_table(供應商文本,產品文本,pprice整數,cprice intege t
SQL解析或執行時出錯 。
問題在這里
private static final String CREATE_QUERY="create table"+ProductContract.ProductEntry.TABLE_NAME+
"("+ ProductContract.ProductEntry.VENDOR+ " text,"+ ProductContract.ProductEntry.PRODUCT+ " text,"+
ProductContract.ProductEntry.PPRICE+ " integer,"+ ProductContract.ProductEntry.CPRICE+ " integer);";
您需要在create table
Statement和before each column name
添加額外的space
。
您可以使用"CREATE TABLE IF NOT EXISTS " + ProductContract.ProductEntry.TABLE_NAME+
卸載應用程序並再次運行 。
不要像onPostExcute方法中那樣使用Toast。
這樣做
runOnUiThread(new Runnable() {
public void run() {
try {
Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
} catch (Exception e) {
}
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.