无法将值插入表中

时间:2013-08-04 19:40:00

标签: android sqlite

下面的代码来自UserLoggedInPage.java ...这里当按下存款按钮时,它应该将交易插入到交易表中..

public void onClick(View arg0) {
    // TODO Auto-generated method stub
    convertEditTextVarsIntoStrings();
    boolean depo;

    switch (arg0.getId()) {
    case R.id.bBalance:
        Log.d(TAG, "Pressed Show Balance");
        try {
            showBalance(Userbalance);
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        break;
    case R.id.bDeposit:
        Log.d(TAG, "Pressed Deposit");
        depo = true;
        try {
            UA.deposit(DepositAmt);
            Userbalance = String.valueOf(UA.setBalance());
            updateBalanceDatabase(id, Userbalance, UserLoggedInPage.this);
            updateTransactionDatabase(id, DepositAmt, depo,
                    UserLoggedInPage.this);
        } catch (RemoteException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        break;
    case R.id.bWithdraw:
        Log.d(TAG, "Pressed Withdraw");
        depo = false;
        try {
            UA.withdraw(WithdrawAmt);
            Userbalance = String.valueOf(UA.setBalance());
            updateBalanceDatabase(id, Userbalance, UserLoggedInPage.this);
            updateTransactionDatabase(id, WithdrawAmt, depo,
                    UserLoggedInPage.this);
        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        break;
    }
public void updateTransactionDatabase(String id2, String Amt,
            boolean depo, Context context) throws SQLException{
        String add;
        int iddd = Integer.parseInt(id2);
        // TODO Auto-generated method stub
        if (depo == true) {
            add = "Deposit :";
        } else {
            add = "Withdraw :";
        }
        myDb = new DbHelper(context);
        SQLiteDatabase db = myDb.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put("user_id", iddd);
        values.put("trans", add + Amt);

        try {
            db.insert(DbHelper.SUBH_TABLE_DATA, null, values);
            Toast.makeText(getApplicationContext(), "Transaction Sucessful!!",
                    Toast.LENGTH_SHORT).show();
            System.out.println("Inside Transaction");
        } catch (Exception e) {
            e.printStackTrace();
        }

    } 

以下代码来自AdminPage.java

public void onClick(View v) {

    switch (v.getId()) {

    case R.id.buttonLogin:
        mPassword = (EditText) findViewById(R.id.editPassword);

        String pass = mPassword.getText().toString();
        if (pass.equals("") || pass == null) {

            Toast.makeText(getApplicationContext(), "Password Missing",
                    Toast.LENGTH_SHORT).show();

        } else {

            if (pass.equals("admin")) {
                Toast.makeText(getApplicationContext(), "Retrieving Data",
                        Toast.LENGTH_SHORT).show();
                // retrieveEntries();
                retrieveLastTransaction();
            } else {
                Toast.makeText(getApplicationContext(),
                        "Admin Login Failed", Toast.LENGTH_SHORT).show();
            }
        }

        break;

    case R.id.buttonCancel:
        Intent i = new Intent(AdminPage.this, DatabaseActivity.class);
        startActivity(i);
        finish();
        break;
    }
}
public void retrieveLastTransaction() throws SQLException {
    // TODO Auto-generated method stub
    try {

        SQLiteDatabase db = myDb.getReadableDatabase();

        String[] columns = { "user_id", "trans" };

        Cursor cursor = db.query(DbHelper.SUBH_TABLE_DATA, columns, null,
                null, null, null, null);
        if (cursor != null) {
            System.out.println("Transaction showing");
            startManagingCursor(cursor);
            showTransactionDatabase(cursor);
        }
        System.out.println("Cursor NuLL");

    } catch (Exception e) {
        e.printStackTrace();
    }

}

private void showTransactionDatabase(Cursor cursor) throws SQLException {
    // TODO Auto-generated method stub
    StringBuilder ret = new StringBuilder("\tDatabase Values\n\n");
    ret.append("\nID\t\t\t Transaction\t\n");
    while (cursor.moveToNext()) {
        String idddd = cursor.getString(0);

        String transac = cursor.getString(1);

        ret.append(idddd + "\t" + transac + "\n");
    }

    TextView result = new TextView(this);
    result.setText(ret);
    setContentView(result);

}

以下代码来自DbHelper.java,我创建了两个表'login'和'Transaction'表.... 问题是我无法在Transaction表中插入任何数据

private static final String DATABASE_NAME = "saket.db";
private static final int DATABASE_VERSION = 1;
public static final String SUBH_TABLE_NAME = "login";
public static final String SUBH_TABLE_DATA = "Transaction";
public static final String KEY_ROWID = "_id";
private static final String SUBH_TABLE_CREATE =
                "CREATE TABLE " + SUBH_TABLE_NAME + "(" +
                "_id INTEGER PRIMARY KEY AUTOINCREMENT,"+
                "username TEXT NOT NULL, password TEXT NOT NULL, email TEXT NOT NULL, balance INTEGER);";
private static final String SUBH_TABLE_DATA_CREATE =
        "CREATE TABLE " + SUBH_TABLE_DATA + "(" +
        "user_id INTEGER, " +
        "trans TEXT NOT NULL);";

private static final String SAKET_DB_ADMIN = "INSERT INTO "+SUBH_TABLE_NAME +" values(1, admin, password, admin@gmail.com, balance);";


public DbHelper(Context context) {

    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    System.out.println("In constructor");

}

/* (non-Javadoc)
 * @see android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite.SQLiteDatabase)
 */
@Override
public void onCreate(SQLiteDatabase db) {

    try{
        //Create Database
        db.execSQL(SUBH_TABLE_CREATE);

        //create admin account
        db.execSQL(SAKET_DB_ADMIN);

        //create transaction account
        db.execSQL(SUBH_TABLE_DATA_CREATE);
        System.out.println("In onCreate");
    }catch(Exception e){
        e.printStackTrace();
    }
}

这是logcat ..

08-04 23:15:22.582: I/Database(1558): sqlite returned: error code = 1, msg = near "Transaction": syntax error
08-04 23:15:22.582: W/System.err(1558): android.database.sqlite.SQLiteException: near "Transaction": syntax error: , while compiling: SELECT user_id, trans FROM Transaction
08-04 23:15:22.592: W/System.err(1558):     at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
08-04 23:15:22.592: W/System.err(1558):     at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
08-04 23:15:22.602: W/System.err(1558):     at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
08-04 23:15:22.612: W/System.err(1558):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
08-04 23:15:22.612: W/System.err(1558):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46)
08-04 23:15:22.612: W/System.err(1558):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
08-04 23:15:22.612: W/System.err(1558):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
08-04 23:15:22.622: W/System.err(1558):     at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1229)
08-04 23:15:22.632: W/System.err(1558):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1184)
08-04 23:15:22.632: W/System.err(1558):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1264)
08-04 23:15:22.643: W/System.err(1558):     at home.saket.AdminPage.retrieveLastTransaction(AdminPage.java:93)
08-04 23:15:22.643: W/System.err(1558):     at home.saket.AdminPage.onClick(AdminPage.java:68)
08-04 23:15:22.643: W/System.err(1558):     at android.view.View.performClick(View.java:2408)
08-04 23:15:22.643: W/System.err(1558):     at android.view.View$PerformClick.run(View.java:8816)
08-04 23:15:22.643: W/System.err(1558):     at android.os.Handler.handleCallback(Handler.java:587)
08-04 23:15:22.643: W/System.err(1558):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-04 23:15:22.643: W/System.err(1558):     at android.os.Looper.loop(Looper.java:123)
08-04 23:15:22.652: W/System.err(1558):     at android.app.ActivityThread.main(ActivityThread.java:4627)
08-04 23:15:22.652: W/System.err(1558):     at java.lang.reflect.Method.invokeNative(Native Method)
08-04 23:15:22.652: W/System.err(1558):     at java.lang.reflect.Method.invoke(Method.java:521)
08-04 23:15:22.652: W/System.err(1558):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-04 23:15:22.652: W/System.err(1558):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-04 23:15:22.662: W/System.err(1558):     at dalvik.system.NativeStart.main(Native Method)
08-04 23:15:27.502: D/dalvikvm(115): GC_EXPLICIT freed 1043 objects / 58184 bytes in 182ms

0 个答案:

没有答案