-2

我知道这个问题已经被问过很多次了,但相信我,尽管我无法解决错误,但我已经经历了一切。

这是我的错误日志

E/SQLiteLog(4891): (1) no such table: nametable
E/SQLiteDatabase(4891): Error inserting MaximumofHeartRate=90 MaximumofIBI=32 MinimumofHeartRate=40 name=hofmann MinimumofBreathingRate=4 MaximumofBreathingRate=10 patientID=5689063 firstName=peter MinimumofIBI=11
E/SQLiteDatabase(4891): android.database.sqlite.SQLiteException: no such table: nametable (code 1): , while compiling: INSERT INTO nametable(MaximumofHeartRate,MaximumofIBI,MinimumofHeartRate,name,MinimumofBreathingRate,MaximumofBreathingRate,patientID,firstName,MinimumofIBI) VALUES (?,?,?,?,?,?,?,?,?)
E/SQLiteDatabase(4891): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
E/SQLiteDatabase(4891): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)

这是我的代码:

 public class DatabaseHandler extends SQLiteOpenHelper {
         public DatabaseHandler(Context context) {
              super(context, DATABASE_NAME, null, DATABASE_VERSION);
          }
                public SQLiteDatabase db;

        private static final int DATABASE_VERSION = 2;
        private static final String DATABASE_NAME = "dataManager";
        private static final String NAME_TABLE = "nametable";
        private static final String KEY_NAME = "name";
        private static final String KEY_HR = "firstName";
        private static final String KEY_IBI = "patientID";
        private static final String KEY_HMIN = "MinimumofHeartRate";
        private static final String KEY_HMAX = "MaximumofHeartRate";
        private static final String KEY_IMIN = "MinimumofIBI";
        private static final String KEY_IMAX = "MaximumofIBI";
        private static final String KEY_BMIN = "MinimumofBreathingRate";
        private static final String KEY_BMAX = "MaximumofBreathingRate";
            private  static final String TEXT_TYPE = " TEXT";
            private static final String COMMA_SEP = ", ";

         public long insertTitle(String name, String fname, String pid,
                  String hmin, String hmax, String imin, String imax, String bmin, String bmax) 
            {
            SQLiteDatabase db = this.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put(KEY_NAME, name);
                values.put(KEY_HR, fname);
                values.put(KEY_IBI, pid);
                values.put(KEY_HMIN, hmin);
                values.put(KEY_HMAX, hmax);
                values.put(KEY_IMIN, imin);
                values.put(KEY_IMAX, imax);
                values.put(KEY_BMIN, bmin);
                values.put(KEY_BMAX, bmax);

              return db.insert(DatabaseHandler.NAME_TABLE, null, values);
            }

        // Creating Tables
        @Override
        public void onCreate(SQLiteDatabase db) {


                 String DATABASE_CREATE_FIRST ="CREATE TABLE IF NOT EXISTS"+ NAME_TABLE
                          +"(" + KEY_NAME +TEXT_TYPE+ COMMA_SEP +
                          KEY_HR+TEXT_TYPE+ COMMA_SEP  + 
                          KEY_IBI +TEXT_TYPE+ COMMA_SEP+
                          KEY_HMIN+TEXT_TYPE+ COMMA_SEP+
                          KEY_HMAX+TEXT_TYPE+ COMMA_SEP+
                          KEY_IMIN+TEXT_TYPE+ COMMA_SEP+
                          KEY_IMAX+TEXT_TYPE+ COMMA_SEP+
                          KEY_BMIN+TEXT_TYPE+ COMMA_SEP+
                          KEY_BMAX+TEXT_TYPE+ COMMA_SEP+
                         ")";
                db.execSQL(DATABASE_CREATE_FIRST);

        }



  // Upgrading database
          @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

                onCreate(db);
                onUpgrade(db, oldVersion, newVersion);

            }


        public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            onUpgrade(db, oldVersion, newVersion);
        }

    }

这是 FragmentActivity 的代码:

save.setOnClickListener(new OnClickListener() {

            @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            DatabaseHandler db = new DatabaseHandler(getActivity());
            db.getWritableDatabase();
            EditText firstName,name,patientID,minH,maxH,minI,maxI,minB,maxB;
            firstName=(EditText)myFragmentView.findViewById(R.id.FN);
            String firstnameofpatient=firstName.getText().toString();
            name=(EditText)myFragmentView.findViewById(R.id.name);
            String nameofpatient=name.getText().toString();
            patientID=(EditText)myFragmentView.findViewById(R.id.PID);
            String enterpatientid=patientID.getText().toString(); 
            minH=(EditText)myFragmentView.findViewById(R.id.Hmin);
            String numHM=minH.getText().toString();
            maxH=(EditText)myFragmentView.findViewById(R.id.Hmax);
            String numHX=maxH.getText().toString();
            minI=(EditText)myFragmentView.findViewById(R.id.Imin);
            String numIM=minI.getText().toString();
            maxI=(EditText)myFragmentView.findViewById(R.id.Imax);
            String numIX=maxI.getText().toString();
            minB=(EditText)myFragmentView.findViewById(R.id.Bmin);
            String numBM=minB.getText().toString();
            maxB=(EditText)myFragmentView.findViewById(R.id.Bmax);
            String numBX=maxB.getText().toString();

            db.insertTitle(firstnameofpatient,nameofpatient,  enterpatientid,numHM,
            numHX,numIM,numIX,numBM,numBX);
            //dataSource.read();

            db.close();

        }
    });
4

1 回答 1

5

onCreate()有 SQL 语法错误:

String DATABASE_CREATE_FIRST ="CREATE TABLE IF NOT EXISTS"+ NAME_TABLE
                      +"(" + KEY_NAME +TEXT_TYPE+ COMMA_SEP +
                      KEY_HR+TEXT_TYPE+ COMMA_SEP  + 
                      KEY_IBI +TEXT_TYPE+ COMMA_SEP+
                      KEY_HMIN+TEXT_TYPE+ COMMA_SEP+
                      KEY_HMAX+TEXT_TYPE+ COMMA_SEP+
                      KEY_IMIN+TEXT_TYPE+ COMMA_SEP+
                      KEY_IMAX+TEXT_TYPE+ COMMA_SEP+
                      KEY_BMIN+TEXT_TYPE+ COMMA_SEP+
                      KEY_BMAX+TEXT_TYPE+ COMMA_SEP+
                     ")";
  • EXISTS和表名之间没有空格。

  • 最后一列规范后的额外逗号。

所以:

String DATABASE_CREATE_FIRST ="CREATE TABLE IF NOT EXISTS "+ NAME_TABLE
                      +"(" + KEY_NAME +TEXT_TYPE+ COMMA_SEP +
                      KEY_HR+TEXT_TYPE+ COMMA_SEP  + 
                      KEY_IBI +TEXT_TYPE+ COMMA_SEP+
                      KEY_HMIN+TEXT_TYPE+ COMMA_SEP+
                      KEY_HMAX+TEXT_TYPE+ COMMA_SEP+
                      KEY_IMIN+TEXT_TYPE+ COMMA_SEP+
                      KEY_IMAX+TEXT_TYPE+ COMMA_SEP+
                      KEY_BMIN+TEXT_TYPE+ COMMA_SEP+
                      KEY_BMAX+TEXT_TYPE+
                     ")";

onCreate()更改数据库架构以强制再次调用时清理您的应用程序数据。


编辑后添加评论:你onUpgrade()也坏了。它无法对现有数据库进行任何修改,并将导致无限递归(导致StackoverflowError)。简单但丢失数据的解决方案是DROP TABLE tablename然后调用onCreate().

于 2013-09-25T11:22:59.830 回答