0

db.insert(table_name,null,contentValues);总是返回-1并且它没有在数据库中插入值。

你能帮我弄清楚我做错了什么吗?

数据库类.java:

public class database_class extends SQLiteOpenHelper {
    public static final String database_name = "signup_db";
    public static final String table_name = "signup";
    public static final String column_1 = "FIRST_NAME";
    public static final String column_2 = "SECOND_NAME";
    public static final String column_3 = "EMAIL";
    public static final String column_4 = "PASSWORD";
    public static final String column_5 = "c_password";

    public database_class(Context context){
        super(context,database_name , null, 1);
        //Toast.makeText(context, "Class called ", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table" + table_name + "(ID INTEGER PRIMARY KEY AUTOINCREMENT , FIRST_NAME TEXT , SECOND_NAME TEXT , EMAIL TEXT , PASSWORD TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        db.execSQL("DROP TABLE IF EXISTS" + table_name);
        onCreate(db);
    }
    public boolean insertData(String first_name, String second_name, String email, String password){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(column_1,first_name);
        contentValues.put(column_2,second_name);
        contentValues.put(column_3,email);
        contentValues.put(column_4,password);
        long result = db.insert(table_name,null,contentValues);
        if (result == -1)
            return false;
        else
            return true;

    }
}

主要活动代码:

fn = editText1.getText().toString();
ln = editText2.getText().toString();
em = editText3.getText().toString();
pass = editText4.getText().toString();
cpass = editText5.getText().toString();

if (1 == 1) {
    //boolean isInserted =  mydb.insertData(signup_values[0].toString(),signup_values[1].toString(),signup_values[2].toString(),signup_values[3].toString());
    boolean isInserted =  mydb.insertData(fn,ln,em,pass);
    if(isInserted == true) {
        Toast.makeText(MainActivity.this, "Data Inserted", Toast.LENGTH_SHORT).show();
    }
    else
        Toast.makeText(MainActivity.this, "Data not Inserted", Toast.LENGTH_SHORT).show();
}
else
    Toast.makeText(MainActivity.this, "Password Mismatch", Toast.LENGTH_SHORT).show();
4

1 回答 1

4

我相信创建数据库时出现错误。这样,create表会失败,然后insert也会失败(因为表没有正确创建)。

您的代码的其他部分似乎还可以...

这一行,将产生以下字符串:

db.execSQL("create table" + table_name + "(ID INTEGER PRIMARY KEY AUTOINCREMENT , FIRST_NAME TEXT , SECOND_NAME TEXT , EMAIL TEXT , PASSWORD TEXT)");

结果:

create tablesignup(ID INTEGER PRIMARY KEY AUTOINCREMENT , FIRST_NAME TEXT , SECOND_NAME TEXT , EMAIL TEXT , PASSWORD TEXT)

这不是有效的 SQL 命令。因此,尝试添加一些空格,例如:

db.execSQL("create table " + table_name + " ( ID INTEGER PRIMARY KEY AUTOINCREMENT, FIRST_NAME TEXT, SECOND_NAME TEXT, EMAIL TEXT, PASSWORD TEXT )");

错误 2

onUpgrade()由于同样的错误,您也将无法工作:

db.execSQL("DROP TABLE IF EXISTS" + table_name);

需要添加一些空格,例如:

db.execSQL("DROP TABLE IF EXISTS " + table_name);

最后

我测试了你的代码并且工作正常。也许,之前发生了一些错误,“重新安装”数据库会很好。

你可以怎么做:

  • 卸载/安装您的应用程序

  • 更新您的数据库版本以强制onUpgrade(). 只是做:super(context, database_name , null, 2);onCreate()

于 2016-07-31T17:06:05.787 回答