简体   繁体   中英

Error creating database in SQLite

I am getting an error when trying to save data in SQLite database. My application has two EditText fields, when user taps 'save' the data entered have to be saved in database.

I am using android studio 3.0.1. Bellow is the error I am getting:

16:13:01.270 12710-12710/com.vysh.gridlayoutmanagerrecyclerview 
E/SQLiteLog: (1) no such table: myTable
02-03 16:13:01.277 12710-12710/com.vysh.gridlayoutmanagerrecyclerview 
E/SQLiteDatabase: Error inserting Password=123 Name=luane

android.database.sqlite.SQLiteException: no such table: myTable (code 
1): , while compiling: INSERT INTO myTable(Password,Name) VALUES (?,?)

at 
android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native 
Method)

When user taps 'save' button, the method insertData() is called. Bellow is the class which contains this method:

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

class MyDbAdapter {
    MyDbHelper helper;

    public MyDbAdapter(Context context)
    {
        helper = new MyDbHelper(context);
    }

public long insertData(String name, String password)
{
    SQLiteDatabase db = helper.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(MyDbHelper.NAME, name);
    contentValues.put(MyDbHelper.PASSWORD, password);
    long id = db.insert(MyDbHelper.TABLE_NAME, null , contentValues);
    //Toast.makeText(context, message, Toast.LENGTH_LONG).show();
    return id;
}

static class MyDbHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "myDatabase";
    private static final String TABLE_NAME = "myTable";
    private static final int DATABASE_Version = 1;
    private static final String UID="_id";
    private static final String NAME = "Name";
    private static final String PASSWORD= "Password";
    private static final String CREATE_TABLE = "CREATE TABLE "+TABLE_NAME +
            "( "+UID+" INTEGER PRIMARY KEY AUTOINCREMENT ," +NAME+ "VARCHAR(225)" + PASSWORD+"VARCHAR(225));";
    // private static final String DROP_TABLE ="DROP TABLE IF EXISTS "+TABLE_NAME;
    private Context context;

    public MyDbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_Version);
        this.context=context;
        Message.message(context,"Started...");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try {

            db.execSQL(CREATE_TABLE);
            Message.message(context,"TABLE CREATED");
        } catch (Exception e) {
            Message.message(context,""+e);
        }
      }
    }// end of MyDbHelper
}

Any help is welcome

You have omitted spaces between column names and the column types. This will result in the table not being created due to the syntax error (a column name cannot contain ( )).

The reason why this is not detected until you click the save button is that the database will not be created/opened until either getReabableDatabase or getWriteableDatabase is called.

You should change :-

private static final String CREATE_TABLE = "CREATE TABLE "+TABLE_NAME +
            "( "+UID+" INTEGER PRIMARY KEY AUTOINCREMENT ," +NAME+ "VARCHAR(225)" + PASSWORD+"VARCHAR(225));";

To be something like :-

private static final String CREATE_TABLE = "CREATE TABLE "+TABLE_NAME +
            "( "+UID+" INTEGER PRIMARY KEY AUTOINCREMENT ," +NAME+ " VARCHAR(225)" + PASSWORD+" VARCHAR(225));";

Note that you will need to delete the App's data or uninstall the App after making the changes, so that the database is deleted and thus allowing the onCreate method to be run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM