将数据保存到数据库时,Android应用程序崩溃

时间:2014-05-06 07:57:56

标签: java android sqlite adapter

将数据保存到数据库时Android应用程序崩溃

这是我从用户那里捕获数据的类。第一个按钮按预期工作,但第二个按钮导致崩溃。

public class ActualSalesTracker extends Activity implements OnClickListener {

    DbAdapter db = new DbAdapter(this);
    Button BtnSalesCal, BtnAddRecordDB, BtnViewRecordsDB;
    EditText item_name, item_cost, item_price_value, item_postage, actual_pl;
    SalesProfitLossCal actSalesCal = new SalesProfitLossCal();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.actual_sales_tracker);

        Button salesCalBtn = (Button) findViewById(R.id.BtnSalesCal);
        // register the click event with the sales calculating profit/loss button
        salesCalBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                // get EditText by id to represent the value of the item stock
                // cost and store it as a double
                EditText itemCostString = (EditText) findViewById(R.id.item_cost);
                String ic = itemCostString.getText().toString();
                double itemCost = Double.valueOf(ic).doubleValue();

                // get EditText by id to represent the value of the post and
                // packaging cost and store it as a double
                EditText itemPostageString = (EditText) findViewById(R.id.item_postage);
                String ipapc = itemPostageString.getText().toString();
                double itemPostage = Double.valueOf(ipapc).doubleValue();

                // get EditText by id to represent the value of the selling
                // price and store it as a double
                EditText itemPriceValueString = (EditText) findViewById(R.id.item_price_value);
                String sp = itemPriceValueString.getText().toString();
                double itemPriceValue = Double.valueOf(sp).doubleValue();

                double actTotalCost = actSalesCal.ActTotalCostCal(itemCost,
                        itemPostage, itemPriceValue);
                double actualProfitLoss = actSalesCal
                        .calculateProfitLossGenerated(itemPriceValue,
                                actTotalCost);

                String ActualProfitLossString = String.format(
                        "You made £ %.2f", actualProfitLoss);
                TextView ActualProfitLossView = (TextView) findViewById(R.id.yourActualPL);
                ActualProfitLossView.setText(ActualProfitLossString);
            }

        });



        //activate the add record button
        Button addRecordDB = (Button) findViewById(R.id.BtnAddRecordDB);
        // register the click event with the add record button
        addRecordDB.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                EditText nameText= (EditText)findViewById(R.id.item_name);
                String name = nameText.getText().toString();
                EditText costText=(EditText)findViewById(R.id.item_cost);
                String cost=costText.getText().toString();
                EditText priceText=(EditText)findViewById(R.id.item_price_value);
                String price=priceText.getText().toString();
                EditText postageText=(EditText)findViewById(R.id.item_postage);
                String postage=postageText.getText().toString();
                TextView profitlossText=(TextView)findViewById(R.id.actual_pl);
                String profitloss=profitlossText.getText().toString();

                db.open();
                long id=db.insertRecord(name, cost, price, postage, profitloss);
                db.close();


            }

        });



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

}

这是我的数据库适配器。

public class DbAdapter {

    public static final String KEY_ROWID = "id";
    public static final String KEY_ITEM_NAME = "name";
    public static final String KEY_ITEM_COST = "cost";
    public static final String KEY_ITEM_PRICE_VALUE = "price";
    public static final String KEY_ITEM_POSTAGE = "postage";
    public static final String KEY_ACTUAL_PL = "profitloss";

    private static final String TAG = "DbAdapter";

    private static final String DATABASE_NAME = "eBaySalesDB";
    private static final String DATABASE_TABLE = "actualSales";
    private static final int DATABASE_VERSION = 1;

    private DatabaseHelper mDbHelper;
    private SQLiteDatabase db;

    private static final String DATABASE_CREATE = "create table if not exists"
            + DATABASE_TABLE + "(" + KEY_ROWID
            + "integer primary key autoincrement, " + KEY_ITEM_NAME
            + "text not null," + KEY_ITEM_COST + "text not null,"
            + KEY_ITEM_PRICE_VALUE + "text not null," + KEY_ITEM_POSTAGE
            + "text not null," + KEY_ACTUAL_PL + "text not null);";

    private final Context mCtx;

    private static class DatabaseHelper extends SQLiteOpenHelper {
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("Drop table if exists " + DATABASE_TABLE);
            onCreate(db);
        }

    }

    /**
     * constructor - takes the context to allow the database to be opened /
     * created
     * 
     * @param ctx
     */
    public DbAdapter(Context ctx) {
        this.mCtx = ctx;
    }

    /**
     * open up the database. If it cannot be opened it will try to create a new
     * instance of the database. if this can't be created it will throw an
     * exception
     * 
     * @return this (self reference)
     * @throws SQLException
     *             (if the database can't be opened or closed.
     */
    public DbAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        db = mDbHelper.getWritableDatabase();
        return this;
    }

    /**
     * Method to close the code off to others
     */
    public void close() {
        mDbHelper.close();
    }

    /**
     * method to insert a record into the database
     */
    public long insertRecord(String name, String cost, String price,
            String postage, String profitloss) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_ITEM_NAME, name);
        initialValues.put(KEY_ITEM_COST, cost);
        initialValues.put(KEY_ITEM_PRICE_VALUE, price);
        initialValues.put(KEY_ITEM_POSTAGE, postage);
        initialValues.put(KEY_ACTUAL_PL, profitloss);
        return db.insert(DATABASE_TABLE, null, initialValues);

    }

    /**
     * method to delete a record from the database
     */
    public boolean deleteRecord(long id) {
        return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + id, null) > 0;
    }

    /**
     * method to retrieve all the records
     */
    public Cursor getAllRecords() {
        return db.query(DATABASE_TABLE, new String[] { KEY_ROWID,
                KEY_ITEM_NAME, KEY_ITEM_COST, KEY_ITEM_PRICE_VALUE,
                KEY_ITEM_POSTAGE, KEY_ACTUAL_PL }, null, null, null, null,
                null, null);

    }

    /**
     * method to retrieve a particular record
     */
    public Cursor getRecord(long id) throws SQLException {
        Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
                KEY_ROWID, KEY_ITEM_NAME, KEY_ITEM_COST, KEY_ITEM_PRICE_VALUE,
                KEY_ITEM_POSTAGE, KEY_ACTUAL_PL }, KEY_ROWID + "=" + id, null,
                null, null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    /**
     * method to update a record
     */
    public boolean updateRecord(long id, String name, String cost,
            String price, String postage, String profitloss) {
        ContentValues args = new ContentValues();
        args.put(KEY_ITEM_NAME, name);
        args.put(KEY_ITEM_COST, cost);
        args.put(KEY_ITEM_PRICE_VALUE, price);
        args.put(KEY_ITEM_POSTAGE, postage);
        args.put(KEY_ACTUAL_PL, profitloss);
        return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + id, null) > 0;
    }
}

Logcat错误日志:

05-06 03:55:58.690: E/AndroidRuntime(1251): FATAL EXCEPTION: main
05-06 03:55:58.690: E/AndroidRuntime(1251): Process: com.example.eventbuilder, PID: 1251
05-06 03:55:58.690: E/AndroidRuntime(1251): android.database.sqlite.SQLiteException: near "existsactualSales": syntax error (code 1): , while compiling: create table if not existsactualSales(idinteger primary key autoincrement, nametext not null,costtext not null,pricetext not null,postagetext not null,profitlosstext not null);
05-06 03:55:58.690: E/AndroidRuntime(1251):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
05-06 03:55:58.690: E/AndroidRuntime(1251):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
05-06 03:55:58.690: E/AndroidRuntime(1251):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
05-06 03:55:58.690: E/AndroidRuntime(1251):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
05-06 03:55:58.690: E/AndroidRuntime(1251):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
05-06 03:55:58.690: E/AndroidRuntime(1251):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
05-06 03:55:58.690: E/AndroidRuntime(1251):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1672)
05-06 03:55:58.690: E/AndroidRuntime(1251):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
05-06 03:55:58.690: E/AndroidRuntime(1251):     at com.example.eventbuilder.DbAdapter$DatabaseHelper.onCreate(DbAdapter.java:51)
05-06 03:55:58.690: E/AndroidRuntime(1251):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
05-06 03:55:58.690: E/AndroidRuntime(1251):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
05-06 03:55:58.690: E/AndroidRuntime(1251):     at com.example.eventbuilder.DbAdapter.open(DbAdapter.java:85)
05-06 03:55:58.690: E/AndroidRuntime(1251):     at com.example.eventbuilder.ActualSalesTracker$2.onClick(ActualSalesTracker.java:92)
05-06 03:55:58.690: E/AndroidRuntime(1251):     at android.view.View.performClick(View.java:4438)
05-06 03:55:58.690: E/AndroidRuntime(1251):     at android.view.View$PerformClick.run(View.java:18422)
05-06 03:55:58.690: E/AndroidRuntime(1251):     at android.os.Handler.handleCallback(Handler.java:733)
05-06 03:55:58.690: E/AndroidRuntime(1251):     at android.os.Handler.dispatchMessage(Handler.java:95)
05-06 03:55:58.690: E/AndroidRuntime(1251):     at android.os.Looper.loop(Looper.java:136)
05-06 03:55:58.690: E/AndroidRuntime(1251):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-06 03:55:58.690: E/AndroidRuntime(1251):     at java.lang.reflect.Method.invokeNative(Native Method)
05-06 03:55:58.690: E/AndroidRuntime(1251):     at java.lang.reflect.Method.invoke(Method.java:515)
05-06 03:55:58.690: E/AndroidRuntime(1251):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-06 03:55:58.690: E/AndroidRuntime(1251):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-06 03:55:58.690: E/AndroidRuntime(1251):     at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:1)

您需要在标识符和关键字之间使用空格:

private static final String DATABASE_CREATE = "create table if not exists"
        + DATABASE_TABLE + "(" + KEY_ROWID
        + "integer primary key autoincrement, " + KEY_ITEM_NAME
        + "text not null," + KEY_ITEM_COST + "text not null,"
        + KEY_ITEM_PRICE_VALUE + "text not null," + KEY_ITEM_POSTAGE
        + "text not null," + KEY_ACTUAL_PL + "text not null);";

更改为

private static final String DATABASE_CREATE = "create table if not exists "
        + DATABASE_TABLE + "(" + KEY_ROWID
        + " integer primary key autoincrement, " + KEY_ITEM_NAME
        + " text not null," + KEY_ITEM_COST + " text not null,"
        + KEY_ITEM_PRICE_VALUE + " text not null," + KEY_ITEM_POSTAGE
        + " text not null," + KEY_ACTUAL_PL + " text not null);";

答案 1 :(得分:1)

更改此行
private static final String DATABASE_CREATE = "create table if not exists"
        + DATABASE_TABLE + "(" + KEY_ROWID
        + "integer primary key autoincrement, " + KEY_ITEM_NAME
        + "text not null," + KEY_ITEM_COST + "text not null,"
        + KEY_ITEM_PRICE_VALUE + "text not null," + KEY_ITEM_POSTAGE
        + "text not null," + KEY_ACTUAL_PL + "text not null);";

 private static final String DATABASE_CREATE = "create table if not exists "
        + DATABASE_TABLE + "(" + KEY_ROWID
        + "integer primary key autoincrement, " + KEY_ITEM_NAME
        + "text not null," + KEY_ITEM_COST + "text not null,"
        + KEY_ITEM_PRICE_VALUE + "text not null," + KEY_ITEM_POSTAGE
        + "text not null," + KEY_ACTUAL_PL + "text not null);";

您的表名和创建表名语法之间需要空格

答案 2 :(得分:1)

您在关键字和标识符之间缺少空格。它应该是

private static final String DATABASE_CREATE = "create table if not exists "
            + DATABASE_TABLE + "(" + KEY_ROWID
            + " integer primary key autoincrement, " + KEY_ITEM_NAME
            + " text not null," + KEY_ITEM_COST + " text not null,"
            + KEY_ITEM_PRICE_VALUE + " text not null," + KEY_ITEM_POSTAGE
            + " text not null," + KEY_ACTUAL_PL + " text not null);";

此外,如果您在SQL命令中遇到任何语法错误,请尝试在SQLite中运行它们。它将帮助您识别语法中的任何错误。

Theres是SQLite manager

的一个简单的Firefox插件
相关问题