无法将数据插入数据库android logcat错误

时间:2014-01-20 15:06:10

标签: java android

我想将我输入的数据插入到我的数据库中。 但是,它在logcat中给了我这个错误。 我无法将它们存储起来。

01-20 14:28:43.240: E/SQLiteLog(4186): (1) no such table: fuelLog
01-20 14:28:43.260: E/SQLiteDatabase(4186): Error inserting tcost= 20 fuelprice=10 fcon= 11000 odometer=22000 date=12/12/2012 fuelpump=2
01-20 14:28:43.260: E/SQLiteDatabase(4186): android.database.sqlite.SQLiteException: no such table: fuelLog (code 1): , while compiling: INSERT INTO fuelLog(tcost,fuelprice,fcon,odometer,date,fuelpump) VALUES (?,?,?,?,?,?)
01-20 14:28:43.260: E/SQLiteDatabase(4186):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
01-20 14:28:43.260: E/SQLiteDatabase(4186):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
01-20 14:28:43.260: E/SQLiteDatabase(4186):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
01-20 14:28:43.260: E/SQLiteDatabase(4186):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
01-20 14:28:43.260: E/SQLiteDatabase(4186):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
01-20 14:28:43.260: E/SQLiteDatabase(4186):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
01-20 14:28:43.260: E/SQLiteDatabase(4186):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
01-20 14:28:43.260: E/SQLiteDatabase(4186):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
01-20 14:28:43.260: E/SQLiteDatabase(4186):     at com.example.fuellog.DBAdapter.insertLog(DBAdapter.java:89)
01-20 14:28:43.260: E/SQLiteDatabase(4186):     at com.example.fuellog.MainActivity$4.onClick(MainActivity.java:156)
01-20 14:28:43.260: E/SQLiteDatabase(4186):     at android.view.View.performClick(View.java:4204)
01-20 14:28:43.260: E/SQLiteDatabase(4186):     at android.view.View$PerformClick.run(View.java:17355)
01-20 14:28:43.260: E/SQLiteDatabase(4186):     at android.os.Handler.handleCallback(Handler.java:725)
01-20 14:28:43.260: E/SQLiteDatabase(4186):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-20 14:28:43.260: E/SQLiteDatabase(4186):     at android.os.Looper.loop(Looper.java:137)
01-20 14:28:43.260: E/SQLiteDatabase(4186):     at android.app.ActivityThread.main(ActivityThread.java:5041)
01-20 14:28:43.260: E/SQLiteDatabase(4186):     at java.lang.reflect.Method.invokeNative(Native Method)
01-20 14:28:43.260: E/SQLiteDatabase(4186):     at java.lang.reflect.Method.invoke(Method.java:511)
01-20 14:28:43.260: E/SQLiteDatabase(4186):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-20 14:28:43.260: E/SQLiteDatabase(4186):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-20 14:28:43.260: E/SQLiteDatabase(4186):     at dalvik.system.NativeStart.main(Native Method)

这是我的DBAdapter.java

public class DBAdapter {

public static final String KEY_ROWID = "_id";
    public static final String KEY_DATE = "date";
    public static final String KEY_PRICE = "fuelprice";
    public static final String KEY_FUEL = "fuelpump";
    public static final String KEY_COST = "tcost";
    public static final String KEY_ODM = "odometer";
    public static final String KEY_CON = "fcon";

    private static final String TAG = "DBADAPTER";

     static final String DATABASE_NAME = "login.db";
     static final int DATABASE_VERSION = 1;
     static final String DATABASE_TABLE = "fuelLog";

    private static final String DATABASE_CREATE = 
            "create table fuelLog (_id integer primary key auto increment" +
            "date text not null, fuelprice text not null, fuelpump text not null, tcost text not null, odometer text not null, fcon text not null);";


    private final Context context;    

        private DatabaseHelper DBHelper;
        private SQLiteDatabase db;

        public DBAdapter(Context ctx){
            this.context = ctx;
            DBHelper = new DatabaseHelper(context);
        }

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

            public void onCreate(SQLiteDatabase db) 
            {
                try{
                    db.execSQL(DATABASE_CREATE);    
                }catch (SQLException e){
                    e.printStackTrace();
                }
            }//onCreate

            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 fuelLog");
                onCreate(db);

        }
    }

        public DBAdapter open() throws SQLException
        {
            db = DBHelper.getWritableDatabase();
            return this;
        }
        //close database

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

        public long insertLog (String date, String fuelprice, String fuelpump, String tcost , String odometer,String fcon)

        {
            ContentValues initialValues = new ContentValues();
            initialValues.put(KEY_DATE, date);
            initialValues.put(KEY_PRICE, fuelprice);
            initialValues.put(KEY_FUEL, fuelpump);
            initialValues.put(KEY_COST, tcost);
            initialValues.put(KEY_ODM, odometer);
            initialValues.put(KEY_CON, fcon);
        return db.insert(DATABASE_TABLE, null, initialValues);

        }
}//DBAdapter

这是我的mainactivity.java

public class MainActivity extends Activity {

    Button saveButton = null;
    EditText dateEdit; 
    EditText priceEdit;
    EditText pumpEdit;
    TextView costView;
    EditText odometerEdit;
    TextView fconView;
     TextWatcher textWatcher;
     String priceEditStr ="",pumpEditStr="";
     String  odmEditStr = "";
int result;
int resultCon;



    public boolean isNumeric(String str)
    {
        return str.matches("-?\\d+(\\.\\d+)?"); 
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        costView = (TextView)findViewById(R.id.tcost);
        dateEdit = (EditText)findViewById(R.id.date);
        priceEdit = (EditText)findViewById(R.id.fuelprice);
        pumpEdit = (EditText)findViewById(R.id.fuelpump);
        odometerEdit = (EditText)findViewById(R.id.odometer);
        fconView = (TextView)findViewById(R.id.fcon);


           priceEdit.addTextChangedListener(new TextWatcher() {

               @Override
               public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

               }

               @Override
               public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

               }

               @Override
               public void afterTextChanged(Editable editable) {
                  //here, after we introduced something in the EditText we get the string from it
                   if(!priceEdit.getText().toString().trim().equalsIgnoreCase("") && !priceEdit.getText().toString().trim().equalsIgnoreCase(null))
                        priceEditStr = priceEdit.getText().toString().trim();
                   if(!pumpEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase(null))
                        pumpEditStr = pumpEdit.getText().toString().trim();

                  if(!priceEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase(""))
                  {
                    result = Integer.parseInt(priceEditStr) * Integer.parseInt(pumpEditStr);              
                    costView.setText(" "+result);
                  }

               }
           });

           pumpEdit.addTextChangedListener(new TextWatcher() {

               @Override
               public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

               }

               @Override
               public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

               }

               @Override
               public void afterTextChanged(Editable editable) {
                  //here, after we introduced something in the EditText we get the string from it
                   if(!priceEdit.getText().toString().trim().equalsIgnoreCase(""))
                        priceEditStr = priceEdit.getText().toString().trim();
                   if(!pumpEdit.getText().toString().trim().equalsIgnoreCase(""))
                        pumpEditStr = pumpEdit.getText().toString().trim();


                   if(!priceEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase(""))
                      {
                        result = Integer.parseInt(priceEditStr) * Integer.parseInt(pumpEditStr);              
                        costView.setText(" "+result);
                      }

               }
           });





           odometerEdit.addTextChangedListener(new TextWatcher() {
               @Override
               public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

               }

               @Override
               public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

               }

               @Override
               public void afterTextChanged(Editable editable) {
                  //here, after we introduced something in the EditText we get the string from it

                   if(!odometerEdit.getText().toString().trim().equalsIgnoreCase(""))
                       odmEditStr = odometerEdit.getText().toString().trim();


                  if(!odometerEdit.getText().toString().trim().equalsIgnoreCase("") && !pumpEdit.getText().toString().trim().equalsIgnoreCase(""))
                     {
                       resultCon = Integer.parseInt(odmEditStr) / Integer.parseInt(pumpEditStr);              
                       fconView.setText(" "+resultCon);
                     }

               }
           });


        saveButton = (Button) findViewById(R.id.saveBTN);
        saveButton.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                DBAdapter dbAdaptor = new DBAdapter(getApplicationContext());
                try
                {
                    dbAdaptor.open();
                    String date = dateEdit.getText().toString();
                    String price = priceEdit.getText().toString();
                    String pump = pumpEdit.getText().toString();
                    String cost = costView.getText().toString();
                    String odometer = odometerEdit.getText().toString();
                    String fcon = fconView.getText().toString();
                    dbAdaptor.insertLog(date, price, pump, cost, odometer, fcon);

                }
                catch(Exception e){
                    Log.d("Fuel Log", e.getMessage());
                }
                finally
                {
                    if(dbAdaptor != null)
                        dbAdaptor.close();
                }
            }
        });

    }//oncreate


}//main

1 个答案:

答案 0 :(得分:2)

您的create语句错误:

private static final String DATABASE_CREATE = 
            "create table fuelLog (_id integer primary key auto increment" +
            "date text not null, fuelprice text not null, fuelpump text not null, tcost text not null, odometer text not null, fcon text not null);";

必须是:

private static final String DATABASE_CREATE = 
            "create table fuelLog (_id integer primary key auto increment, " +
            "date text not null, fuelprice text not null, fuelpump text not null, tcost text not null, odometer text not null, fcon text not null);";

你忘记第一行和第二行之间的空格和逗号。