致命异常:AsyncTask#4 ... java.lang.RuntimeException:执行doInBackground()

时间:2016-01-12 07:50:38

标签: android sqlite

我的代码中有什么问题?我可以编译,但是当我运行该部件将数据保存到数据库中时。它崩溃了。请帮忙。

  

引起:android.database.sqlite.SQLiteException:near   " tableproduct_table&#34 ;:语法错误(代码1):,编译时:   创建tableproduct_table(供应商文本,产品文本,pprice   整数,cprice intege t   android.database.sqlite.SQLiteConnection.nativePrepareStatement(母语   方法)                                                                      在android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)                                                                      在android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)                                                                      在android.database.sqlite.SQLiteSession.prepare(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) {

    }
}

2 个答案:

答案 0 :(得分:3)

你的logcat返回是什么

  

引起:android.database.sqlite.SQLiteException:near   &#34; tableproduct_table&#34 ;:语法错误(代码1):,编译时:创建   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);";

您需要在space 声明&amp;之后添加额外的 create table before each column name

您可以使用"CREATE TABLE IF NOT EXISTS " + ProductContract.ProductEntry.TABLE_NAME+

卸载app&amp;再次运行

答案 1 :(得分:0)

请勿使用onPostExcute方法中使用的Toast。

这样做

                runOnUiThread(new Runnable() {
                        public void run() {
                            try {
                                Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
                            } catch (Exception e) {

                            }
                        }
                    });