简体   繁体   English

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

[英]Android app crashes when saving data to database

Android app crashes when saving data to database 将数据保存到数据库时,Android应用程序崩溃

This is the class where I am capturing data from the user. 这是我从用户那里捕获数据的类。 The first button is working as expected but the second causes crash. 第一个按钮按预期工作,但第二个导致崩溃。

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

    }

}

This is my database adapter. 这是我的数据库适配器。

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 error log: 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)

You need whitespace between identifiers and keywords here: 您需要在标识符和关键字之间添加空格:

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);";

Change to 改成

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);";

change this line from 更改此行从

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);";

to

 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);";

you need space between your table name and Create table name syntax 在表名和创建表名语法之间需要空格

You are missing spaces in between keywords and identifiers. 您在关键字和标识符之间缺少空格。 It should be 它应该是

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);";

Also in future, if you get any syntax errors in SQL commands try running them in SQLite. 同样在将来,如果您在SQL命令中遇到语法错误,请尝试在SQLite中运行它们。 It will help you identify any errors in syntax. 这将帮助您识别语法上的任何错误。

Theres a simple Firefox addon for SQLite manager 有一个用于SQLite Manager的简单Firefox插件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM