android.database.sqlite.SQLiteException:near";:语法错误(代码1):,编译时:android编程错误

android.database.sqlite.SQLiteException:near";:语法错误(代码1):,编译时:android编程错误,android,sqlite,error-handling,Android,Sqlite,Error Handling,我制作了一个包含两个表的android数据库。第二个尚未初始化,但对于第一个,我得到了这个错误 这是我的日志 02-23 01:55:41.494 855-855/tubapps.budgetdatabase E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{tubapps.budgetdatabase/tubapp

我制作了一个包含两个表的android数据库。第二个尚未初始化,但对于第一个,我得到了这个错误

这是我的日志

02-23 01:55:41.494      855-855/tubapps.budgetdatabase E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{tubapps.budgetdatabase/tubapps.budgetdatabase.MainActivity}: android.database.sqlite.SQLiteException: near "create_income_table": syntax error (code 1): , while compiling: create_income_table income(_id INTEGER PRIMARY KEY AUTOINCREMENT, income_amount TEXT NOT NULL, income_payer TEXT NOT NULL, income_date TEXT NOT NULL, income_category TEXT NOT NULL, income_payments TEXT NOT NULL);
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.database.sqlite.SQLiteException: near "create_income_table": syntax error (code 1): , while compiling: create_income_table income(_id INTEGER PRIMARY KEY AUTOINCREMENT, income_amount TEXT NOT NULL, income_payer TEXT NOT NULL, income_date TEXT NOT NULL, income_category TEXT NOT NULL, income_payments TEXT NOT NULL);
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
            at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
            at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
            at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1663)
            at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
            at tubapps.budgetdatabase.DBHelper.onCreate(DBHelper.java:55)
            at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
            at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
            at tubapps.budgetdatabase.SQLController.open(SQLController.java:21)
            at tubapps.budgetdatabase.MainActivity.onCreate(MainActivity.java:29)
            at android.app.Activity.performCreate(Activity.java:5008)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
这是我的sqlcontroller

public class SQLController {

    private DBHelper dbhelper;
    private Context ourcontext;
    private SQLiteDatabase database;

    public SQLController(Context c) {
        ourcontext = c;
    }

    public SQLController open() throws SQLException {
        dbhelper = new DBHelper(ourcontext);
        database = dbhelper.getWritableDatabase();
        return this;

    }

    public void close() {
        dbhelper.close();
    }

    public void insertIncomeData(String incomeAmount, String incomePayer, String incomeDate,
                           String incomeCategory, String incomePayments) {
        ContentValues cv = new ContentValues();
        cv.put(DBHelper.INCOME_AMOUNT, incomeAmount);
        cv.put(DBHelper.INCOME_PAYER, incomePayer);
        cv.put(DBHelper.INCOME_DATE, incomeDate);
        cv.put(DBHelper.INCOME_CATEGORY, incomeCategory);
        cv.put(DBHelper.INCOME_PAYMENTS, incomePayments);
        database.insert(DBHelper.TABLE_INCOME, null, cv);
    }

    public void insertExpenseData(String expenseAmount, String expensePayee, String expenseDate,
                                  String expenseCategory, String expensePayments) {
        ContentValues cv = new ContentValues();
        cv.put(DBHelper.EXPENSE_AMOUNT, expenseAmount);
        cv.put(DBHelper.EXPENSE_PAYEE, expensePayee);
        cv.put(DBHelper.EXPENSE_DATE, expenseDate);
        cv.put(DBHelper.EXPENSE_CATEGORY, expenseCategory);
        cv.put(DBHelper.EXPENSE_PAYMENTS, expensePayments);
        database.insert(DBHelper.TABLE_EXPENSE, null, cv);
    }

    //Getting Cursor to read data from table
    public Cursor readIncomeData() {
        String[] allColumns = new String[] { DBHelper.INCOME_ID,
                DBHelper.INCOME_AMOUNT, DBHelper.INCOME_PAYER, DBHelper.INCOME_DATE,
                DBHelper.INCOME_CATEGORY, DBHelper.INCOME_PAYMENTS };
        Cursor c = database.query(DBHelper.TABLE_INCOME, allColumns, null,
                null, null, null, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }

    //Getting Cursor to read data from table
    public Cursor readExpenseData() {
        String[] allColumns = new String[] { DBHelper.EXPENSE_ID,
                DBHelper.EXPENSE_AMOUNT, DBHelper.EXPENSE_PAYEE, DBHelper.EXPENSE_DATE,
                DBHelper.EXPENSE_CATEGORY, DBHelper.EXPENSE_PAYMENTS };
        Cursor c = database.query(DBHelper.TABLE_EXPENSE, allColumns, null,
                null, null, null, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }

    //Updating record data into table by id
    public int updateIncomeData(long incomeID, String newIncomeAmount, String newIncomePayer, String newIncomeDate,
                          String newIncomeCategory, String newIncomePayments) {
        ContentValues cvUpdate = new ContentValues();
        cvUpdate.put(DBHelper.INCOME_AMOUNT, newIncomeAmount);
        cvUpdate.put(DBHelper.INCOME_PAYER, newIncomePayer);
        cvUpdate.put(DBHelper.INCOME_DATE, newIncomeDate);
        cvUpdate.put(DBHelper.INCOME_CATEGORY, newIncomeCategory);
        cvUpdate.put(DBHelper.INCOME_PAYMENTS, newIncomePayments);
        int i = database.update(DBHelper.TABLE_INCOME, cvUpdate,
                DBHelper.INCOME_ID + " = " + incomeID, null);
        return i;
    }

    public int updateExpenseData(long expenseID, String newExpenseAmount, String newExpensePayer, String newExpenseDate,
                                String newExpenseCategory, String newExpensePayments) {
        ContentValues cvUpdate = new ContentValues();
        cvUpdate.put(DBHelper.EXPENSE_AMOUNT, newExpenseAmount);
        cvUpdate.put(DBHelper.EXPENSE_PAYEE, newExpensePayer);
        cvUpdate.put(DBHelper.EXPENSE_DATE, newExpenseDate);
        cvUpdate.put(DBHelper.EXPENSE_CATEGORY, newExpenseCategory);
        cvUpdate.put(DBHelper.EXPENSE_PAYMENTS, newExpensePayments);
        int i = database.update(DBHelper.TABLE_EXPENSE, cvUpdate,
                DBHelper.EXPENSE_ID + " = " + expenseID, null);
        return i;
    }

    // Deleting record data from table by id
    public void deleteIncomeData(long incomeID) {
        database.delete(DBHelper.TABLE_INCOME, DBHelper.INCOME_ID + "="
                + incomeID, null);
    }

    public void deleteExpenseData(long expenseID) {
        database.delete(DBHelper.TABLE_EXPENSE, DBHelper.EXPENSE_ID + "="
                + expenseID, null);
    }
}
这是我的主要活动

public class MainActivity extends ActionBarActivity {

    ListView lv;
    SQLController dbcon;
    TextView incomeID_tv, incomePayer_tv, incomeAmount_tv;
    TextView incomeDate_tv, incomeCategory_tv, incomePayments_tv;

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

        dbcon = new SQLController(this);
        dbcon.open();
        lv = (ListView) findViewById(R.id.incomeList_id);

        Cursor cursor = dbcon.readIncomeData();
        String[] from = new String[] { DBHelper.INCOME_ID, DBHelper.INCOME_AMOUNT, DBHelper.INCOME_PAYER,
                DBHelper.INCOME_DATE, DBHelper.INCOME_CATEGORY, DBHelper.INCOME_PAYMENTS};
        int[] to = new int[] { R.id.income_id, R.id.income_amount, R.id.income_payer, R.id.income_date,
                R.id.income_category, R.id.income_payments};

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(
                MainActivity.this, R.layout.income_entry, cursor, from, to);

        adapter.notifyDataSetChanged();
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                incomeID_tv = (TextView) view.findViewById(R.id.income_id);
                incomeAmount_tv = (TextView) view.findViewById(R.id.income_amount);
                incomePayer_tv = (TextView) view.findViewById(R.id.income_payer);
                incomeDate_tv = (TextView) view.findViewById(R.id.income_date);
                incomeCategory_tv = (TextView) view.findViewById(R.id.income_category);
                incomePayments_tv = (TextView) view.findViewById(R.id.income_payments);

                String incomeID = incomeID_tv.getText().toString();
                String incomeAmount = incomeAmount_tv.getText().toString();
                String incomePayer = incomePayer_tv.getText().toString();
                String incomeDate = incomeDate_tv.getText().toString();
                String incomeCategory = incomeCategory_tv.getText().toString();
                String incomePayments = incomePayments_tv.getText().toString();

                Intent modify_intent = new Intent(getApplicationContext(),
                        IncomeEdit.class);
                modify_intent.putExtra("incomeID", incomeID);
                modify_intent.putExtra("newIncomeAmount", incomeAmount);
                modify_intent.putExtra("newIncomePayer", incomePayer);
                modify_intent.putExtra("newIncomeDate", incomeDate);
                modify_intent.putExtra("newIncomeCategory", incomeCategory);
                modify_intent.putExtra("newIncomePayments", incomePayments);
                startActivity(modify_intent);
            }
        });
    }
MainActivity的第29行是:

dbcon.open();

我在其他问题中看到,它可能来自表名。它们之间是有空格的,但我改了之后,还是不起作用

您正在执行命令:

create_income_table income(
    _id INTEGER PRIMARY KEY AUTOINCREMENT,
    income_amount TEXT NOT NULL,
    income_payer TEXT NOT NULL,
    income_date TEXT NOT NULL,
    income_category TEXT NOT NULL,
    income_payments TEXT NOT NULL
);
创建收入表
替换为
创建表
。您需要执行以下SQL:

CREATE TABLE income(
    _id INTEGER PRIMARY KEY AUTOINCREMENT,
    income_amount TEXT NOT NULL,
    income_payer TEXT NOT NULL,
    income_date TEXT NOT NULL,
    income_category TEXT NOT NULL,
    income_payments TEXT NOT NULL
);

例外情况清楚地显示在这一行中

Caused by: android.database.sqlite.SQLiteException: near "create_income_table": syntax error (code 1): , while compiling: create_income_table income(_id INTEGER PRIMARY KEY AUTOINCREMENT, income_amount TEXT NOT NULL, income_payer TEXT NOT NULL, income_date TEXT NOT NULL, income_category TEXT NOT NULL, income_payments TEXT NOT NULL);
使用此行创建表

 // TABLE CREATION STATEMENT
private static final String CREATE_INCOME_TABLE = "create table "
        + TABLE_INCOME + "(" + INCOME_ID
        + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + INCOME_AMOUNT + " TEXT NOT NULL, "
        + INCOME_PAYER + " TEXT NOT NULL, "
        + INCOME_DATE + " TEXT NOT NULL, "
        + INCOME_CATEGORY + " TEXT NOT NULL, "
        + INCOME_PAYMENTS + " TEXT NOT NULL)";
还可以修改第二个表创建命令的代码

Caused by: android.database.sqlite.SQLiteException: near "create_income_table": syntax error (code 1): , while compiling: create_income_table income(_id INTEGER PRIMARY KEY AUTOINCREMENT, income_amount TEXT NOT NULL, income_payer TEXT NOT NULL, income_date TEXT NOT NULL, income_category TEXT NOT NULL, income_payments TEXT NOT NULL);
 // TABLE CREATION STATEMENT
private static final String CREATE_INCOME_TABLE = "create table "
        + TABLE_INCOME + "(" + INCOME_ID
        + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + INCOME_AMOUNT + " TEXT NOT NULL, "
        + INCOME_PAYER + " TEXT NOT NULL, "
        + INCOME_DATE + " TEXT NOT NULL, "
        + INCOME_CATEGORY + " TEXT NOT NULL, "
        + INCOME_PAYMENTS + " TEXT NOT NULL)";