插入数据中的Android SQLite错误?

时间:2013-11-30 07:25:07

标签: android database sqlite database-connection sql-insert

我是Android上的新手。我在数据库中的insert语句有些麻烦,当我运行应用程序时,尚未插入值。请有人帮忙......

public class tourDB{

    private static final String TAG = "tourDB";

    //DB information: It's name, and the table we are using.
    private static final String DATABASE_NAME = "tourDatabase";
    private static final String DATABASE_TABLE = "tourtable";
    private static final int DATABASE_VERSION = 1;

    //DataBase Fields here:
    public static final String KEY_ROWID = "_id";
    public static final String KEY_NAME = "TripName";
    public static final String KEY_REASON = "Reason";
    public static final String KEY_CITY = "City";
    public static final String KEY_SOD = "StartingDate";
    public static final String KEY_AD = "ArrivalDate";
    public static final String KEY_BUDGET = "TotalBudget";
    public static final String KEY_HE = "HotelExp";
    public static final String KEY_TE = "TransportExp";
    public static final String KEY_FE = "FoodExp";
    public static final String KEY_SE = "ShoppingExp";
    public static final String KEY_OE = "OthersExp";
    public static final String KEY_REMAINBUDGET = "RemainingBudget";

    //DataBase Field numbers here (0 = KEY_ROWID, 1=...)
    /*public static final int COL_ROWID = 0;
    public static final int COL_NAME = 1;
    public static final int COL_REASON = 2;
    public static final int COL_CITY = 3;
    public static final int COL_SOD = 4;
    public static final int COL_AD = 5;
    public static final int COL_BUDGET  = 6;
    public static final int COL_HE = 7;
    public static final int COL_TE = 8;
    public static final int COL_FE = 9;
    public static final int COL_SE = 10;
    public static final int COL_OE = 11;
    public static final int COL_REMAININGBUDGET = 12;
    */
    //public static final String[] LIST_KEYS = new String[]{"KEY_ROWID","KEY_NAME","KEY_BUDGET"};

    //public static final String DATABAE_UPGRADE = "DROP TABLE IF EXISTS" + DATABASE_TABLE;

    //Context of application who uses us.
    private DbHelper ourHelper;
    private Context ourContext;
    private SQLiteDatabase ourDatabase;

    // Database Helper Class, Subclass: SQLiteOpenHelper

    private static class DbHelper extends SQLiteOpenHelper{

        public DbHelper(Context context) {

            super(context, DATABASE_NAME, null, DATABASE_VERSION);

        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(" CREATE TABLE " + DATABASE_TABLE + "(" 
                    + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " 
                    + KEY_NAME + " TEXT NOT NULL, " 
                    + KEY_REASON + " TEXT NOT NULL, " 
                    + KEY_CITY + " TEXT, " 
                    + KEY_SOD + " DATETIME, "
                    + KEY_AD + " DATETIME, "
                    + KEY_BUDGET + " TEXT not null, " 
                    + KEY_HE + " INTEGER, "
                    + KEY_TE + " INTEGER, "
                    + KEY_FE + " INTEGER, "
                    + KEY_SE + " INTEGER, " 
                    + KEY_OE + " INTEGER, " 
                    + KEY_REMAINBUDGET + "INTEGER);"
            );  
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS" + DATABASE_TABLE);
            onCreate(db);
        }

    }
        public tourDB(Context c){

            this.ourContext = c;
        }

    // Open And Close Method

        public tourDB open() throws SQLException{

            ourHelper = new DbHelper(ourContext);
            ourDatabase = ourHelper.getWritableDatabase();
            return this;

        }

        public void close(){

            ourHelper.close();

        }

        public long createEntry(String TripName, String TripReason, String cityName, String SD, String AD, String budget, 
                                int HE, int TE, int FE, int SE, int OE, int remainBudget) {

            ContentValues v = new ContentValues();
            v.put(KEY_NAME, TripName);
            v.put(KEY_REASON, TripReason);
            v.put(KEY_CITY, cityName);
            v.put(KEY_SOD, SD);
            v.put(KEY_AD, AD);
            v.put(KEY_BUDGET, budget);
            v.put(KEY_HE, HE);
            v.put(KEY_TE, TE);
            v.put(KEY_FE, FE); 
            v.put(KEY_SE, SE);
            v.put(KEY_OE, OE);
            v.put(KEY_REMAINBUDGET, remainBudget);

            ourDatabase.insert(DATABASE_TABLE,null,v);
            return 0;
        }
}

我正在发送数据,但它没有用。显示表没有列Column_name的错误。 这是我发送数据的类。

public void onClick(View arg0) {
        switch(arg0.getId()) {  

        case R.id.button_getdate:


            //editText_startingdate.setText(newtime);

            break;
        case R.id.button_createNew:

            boolean didItWork = true;

            cityName = editText_cityactivity.getText().toString();
            SD = editText_startingdate.getText().toString();
            AD = null;
            budget = editText_budget.getText().toString();
            HE = TE = FE = SE = OE = 0;
            remainBudget = Integer.parseInt(budget);



            try {
                entry.open();
                entry.createEntry(TripName, TripReason, cityName, SD, AD, budget, HE, TE, FE, SE, OE, remainBudget);
                entry.close();

            } catch (Exception e) {
                didItWork = false;
                String error = e.toString();
                Dialog d = new Dialog(this);
                d.setTitle("Error!");
                TextView tv = new TextView(this);
                tv.setText(error);
                d.setContentView(tv);
                d.show();
            } finally {
                if (didItWork) {
                    Dialog d = new Dialog(this);
                    d.setTitle("New Profile" + "Successfully Created");
                    TextView tv = new TextView(this);
                    tv.setText("Success");
                    d.setContentView(tv);
                    d.show();

                    Intent openMainActivity = new Intent("com.blackcat.blackcattourorganizer.MAINACTIVITY");
                    startActivity(openMainActivity);

                }
            }
            break;
        case R.id.button_back:

            Intent openCreateNewActivity_1 = new Intent("com.blackcat.blackcattourorganizer.CREATENEWACTIVITY_1");
            startActivity(openCreateNewActivity_1);

            break;
        }

    }

我的代码中没有发现错误。可以AnyOne帮助

1 个答案:

答案 0 :(得分:0)

db.execSQL(" CREATE TABLE " + DATABASE_TABLE + "(" 
           ...
           + KEY_OE + " INTEGER, "
           + KEY_REMAINBUDGET + "INTEGER);"

最后一列的名称是RemainingBudgetINTEGER。 你忘记了一个太空角色。