简体   繁体   English

在SQLite中创建数据库时出错

[英]Error creating database in SQLite

I am getting an error when trying to save data in SQLite database. 尝试在SQLite数据库中保存数据时出现错误。 My application has two EditText fields, when user taps 'save' the data entered have to be saved in database. 我的应用程序有两个EditText字段,当用户点击“保存”时,输入的数据必须保存在数据库中。

I am using android studio 3.0.1. 我正在使用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. 当用户点击“保存”按钮时,将调用insertData()方法。 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. 在单击“保存”按钮之前,未检测到此问题的原因是,只有调用getReabableDatabasegetWriteableDatabase才能创建/打开数据库。

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. 请注意,进行更改后,您将需要删除应用程序的数据或卸载应用程序,以便删除数据库,从而允许运行onCreate方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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