-1

Just need to make a table and put info in it. Make some code but it gives me error

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)

And my code is

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

    }
}
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Boris Ruzanov
  • 105
  • 1
  • 10

1 Answers1

0

Try to uninstall the application and re install into the emulator and check the problem exist . If exsist then open the adb shell to the emulator and do the following steps.

  1. Open command prompt and type adb shell.
  2. once the shell of the emulator opens -- > then type runas <\package name\>
  3. cd /database
  4. In this location check you have the database created. if created then open it using the sqlite3 <\database_name\>.db .
  5. Type in the queries and check weather the columns are created properly.
  6. If the columns are not found then check out your table creation statement properly. Might have some small issue. Try giving the column name as emailId instead of email and check.
PranavKAndro
  • 921
  • 7
  • 9