【问题标题】:Why field doesnìt create? [duplicate]为什么场不创造? [复制]
【发布时间】:2017-07-08 04:50:51
【问题描述】:

只需要制作一张表格并将信息放入其中。编写一些代码,但它给了我错误

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

    }
}
}

【问题讨论】:

  • 卸载您的应用程序..再次安装检查一次。

标签: java android sql sqlite android-sqlite


【解决方案1】:

尝试卸载应用程序并重新安装到模拟器中并检查问题是否存在。如果存在则打开 adb shell 到模拟器并执行以下步骤。

  1. 打开命令提示符并输入 adb shell。
  2. 一旦模拟器的外壳打开 --> 然后输入 runas
  3. cd /数据库
  4. 在此位置检查您是否已创建数据库。如果已创建,则使用 sqlite3 .db 打开它。
  5. 输入查询并检查列是否正确创建。
  6. 如果未找到列,请正确检查表创建语句。可能有点小问题。尝试将列名称指定为 emailId 而不是 email 并检查。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-18
    • 2021-02-18
    • 2014-01-14
    • 1970-01-01
    • 1970-01-01
    • 2010-10-27
    • 2012-11-28
    相关资源
    最近更新 更多