已创建数据库但未创建数据库表

时间:2014-01-21 06:56:58

标签: android database database-table

我有这组编码,用于将我键入的数据插入到数据库中。 但是,当我导出数据库时,我没有看到任何表

我想知道我是否正确地完成了它。 没有显示任何错误。

logcat的

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

这是我的编码

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 = "fuelLogDB";
     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

3 个答案:

答案 0 :(得分:1)

在你的DATABASE_CREATE字符串中,它应该是'autoincrement',即没有任何空格或下划线。

此外,要么在重新运行应用程序之前完全卸载应用程序,要么将数据库版本更改为2:

static final int DATABASE_VERSION = 2;

因为你的代码已经通过SQLiteOpenHelper中的onCreate()(你捕获了该方法中可能出现的任何错误),我怀疑它没有运行。执行上述操作应该强制它再次执行此操作。最后,删除助手的onCreate()中的try / catch。任何创建表的失败都会导致您的应用程序崩溃,从而导致您需要在logcat中看到错误。

答案 1 :(得分:0)

你的sqlite指向哪个数据库? 如果您在测试设备上运行此代码,那么我认为您无法按原样查看该数据库,因为它将在只能由您的应用程序访问的私有区域中创建。

您确定没有指向错误的数据库吗?

答案 2 :(得分:0)

更改您的创建数据库查询顶部>

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