2017-07-08 128 views
-1

只需要製作一張表並在其中放入信息即可。使一些代碼,但它給了我錯誤爲什麼領域不創造?

E/SQLiteLog: (1) table tableOne has no column named email 
E/SQLiteDatabase: Error inserting email=Email1 name=Name1 
        android.database.sqlite.SQLiteException: table tableOne has no column named email (code 1): , while compiling: INSERT INTO tableOne(email,name) VALUES (?,?) 
         at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
         at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887) 
         at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498) 

而且我的代碼是

public class MainActivity extends AppCompatActivity implements View.OnClickListener{ 

final String LOG_TAG = "myLogs"; 

Button btnAdd; 
Button btnRead; 
EditText editNameField; 
EditText editEmailField; 

DBHelper dbHelper; 

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

    btnAdd = (Button) findViewById(R.id.btn_add); 
    btnRead = (Button) findViewById(R.id.btn_read); 
    editNameField = (EditText) findViewById(R.id.edit_name); 
    editEmailField = (EditText) findViewById(R.id.edit_email); 

    dbHelper = new DBHelper(this); 

    btnAdd.setOnClickListener(this); 
    btnRead.setOnClickListener(this); 


} 

@Override 
public void onClick(View view) { 

    ContentValues cv = new ContentValues(); 

    String name = editNameField.getText().toString(); 
    String email = editEmailField.getText().toString(); 

    SQLiteDatabase db = dbHelper.getWritableDatabase(); 

    switch (view.getId()){ 
     case R.id.btn_add: 
      Log.d(LOG_TAG, "--- Insert in mytable: ---"); 

      cv.put("name", name); 
      cv.put("email", email); 

      long rowID = db.insert("tableOne", null, cv); 
      Log.d(LOG_TAG, "row inserted, ID = " + rowID); 
      break; 
     case R.id.btn_read: 
      Log.d(LOG_TAG, "--- Rows in mytable: ---"); 

      Cursor c = db.query("tableOne", 
        null, 
        null, 
        null, 
        null, 
        null, 
        null 
        ); 

      if (c.moveToFirst()){ 

       int idColIndex = c.getColumnIndex("id"); 
       int nameColIndex = c.getColumnIndex("name"); 
       int emailColIndex = c.getColumnIndex("email"); 

       do { 
        Log.d(LOG_TAG, 
          "ID = " + c.getInt(idColIndex) + 
            ", name = " + c.getString(nameColIndex) + 
            ", email = " + c.getString(emailColIndex)); 
       } while (c.moveToNext()); 
      } else { 
       Log.d(LOG_TAG, "0 rows"); 
       c.close(); 
       break; 
      } 

    } 
} 

class DBHelper extends SQLiteOpenHelper{ 

    public DBHelper(Context context) { 
     super(context, "tableOne", null, 1); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     Log.d(LOG_TAG, "--- onCreate database ---"); 

     db.execSQL("create table tableOne (" 
       + "id integer primary key autoincrement," 
       + "name text," 
       + "email text" + ");"); 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { 

    } 
} 
} 
+2

卸載您的應用程序..安裝再次檢查一次。 –

回答

0

嘗試卸載應用程序,並重新安裝到仿真器和檢查問題存在。如果存在,然後打開adb shell到模擬器並執行以下步驟。

  1. 打開命令提示符並鍵入adb shell。
  2. 一次仿真器的外殼打開 - >然後鍵入runas < \包名\>
  3. CD /數據庫
  4. 在這個位置檢查已經創建的數據庫。如果已創建,則使用sqlite3 < \ database_name \> .db打開它。
  5. 鍵入查詢並檢查天氣欄是否正確創建。
  6. 如果未找到列,請正確檢查表創建語句。可能有一些小問題。嘗試將列名稱設置爲emailId而不是電子郵件並進行檢查。