无法将数据插入android studio sqlite数据库

问题描述 投票:0回答:3

我目前正在做一个学校项目,尝试创建食品配送服务并需要一个注册/登录数据库。但在创建数据/将数据插入数据库时遇到问题。应用程序没有崩溃,但 Toast 消息告诉我“注册失败。”。

从错误日志来看,我似乎能够创建表,但无法将数据插入表中。

如果有人能给我一些建议,我将不胜感激。

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = " mylist.db ";
public static final String TABLE_NAME = " mylist ";
public static final String USER_NAME = " username ";
public static final String EMAIL = " email ";
public static final String PHONE_NUMBER = " phone ";
public static final String PASSWORD = " password ";

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

//CREATING THE DATABASE TABLE
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    String createTable;
    createTable = "CREATE TABLE " + TABLE_NAME +
            " ( ID INTEGER PRIMARY KEY AUTOINCREMENT, "+
            USER_NAME + " username " +
            EMAIL + " email ," +
            PHONE_NUMBER + " phone ,"+
            PASSWORD + " password ) ";
    sqLiteDatabase.execSQL(createTable);
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
}

public boolean addData(String username, String email, String phone, String password){
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(USER_NAME, username);
contentValues.put(EMAIL, email);
contentValues.put(PHONE_NUMBER, phone);
contentValues.put(PASSWORD, password);
long result = sqLiteDatabase.insert(TABLE_NAME, null, contentValues);
    if (result == -1) return false;
else {return true;}
}

}

错误日志

    E/SQLiteLog: (1) table mylist has no column named email
E/SQLiteDatabase: Error inserting  phone =  email =  password =  username =j
    android.database.sqlite.SQLiteException: table mylist has no column named email (code 1): , while compiling: INSERT INTO  mylist ( phone , email , password , username ) VALUES (?,?,?,?)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:890)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:501)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1546)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1417)
        at com.example.doonfood.DatabaseHelper.addData(DatabaseHelper.java:45)
        at com.example.doonfood.signuppage.ADDDATA(signuppage.java:33)
        at com.example.doonfood.signuppage.Signup(signuppage.java:48)
        at java.lang.reflect.Method.invoke(Native Method)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
        at android.view.View.performClick(View.java:6294)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
        at android.view.View$PerformClick.run(View.java:24770)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

signuppage.java

public class signuppage extends AppCompatActivity {

DatabaseHelper myDB;
EditText SignupUserName;
EditText SignupEMail;
EditText SignupPhoneNumber;
EditText SignupPassword;
Button SignupBtn;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.signuppage);
    myDB = new DatabaseHelper(this);
    SignupUserName = findViewById(R.id.SignupUserName);
    SignupEMail = findViewById(R.id.SignupEMail);
    SignupPhoneNumber = findViewById(R.id.SignupPhoneNumber);
    SignupPassword = findViewById(R.id.SignupPassword);
    SignupBtn = findViewById(R.id.SignupBtn);
}

public void ADDDATA(String newUSER, String newEMAIL, String newPHONE, String newPASSWORD){
    boolean insertData = myDB.addData(newUSER, newEMAIL, newPHONE, newPASSWORD);
    if (insertData == true){
        Toast.makeText(signuppage.this, "Sign up successful.", Toast.LENGTH_SHORT).show();
    }else {
        Toast.makeText(signuppage.this, "Sign up unsuccessful.", Toast.LENGTH_SHORT).show();
    }
}

public void Signup(View view) {
    String newUSER, newEMAIL, newPHONE, newPASSWORD;
    newUSER = SignupUserName.getText().toString();
    newEMAIL = SignupEMail.getText().toString();
    newPHONE = SignupPhoneNumber.getText().toString();
    newPASSWORD = SignupPassword.getText().toString();
    if ((SignupUserName.length() != 0) || (SignupEMail.length() != 0) || (SignupPhoneNumber.length() != 0) || (SignupPassword.length() != 0)) {
        ADDDATA(newUSER, newEMAIL, newPHONE, newPASSWORD);
        SignupUserName.setText(newUSER);
        SignupEMail.setText(newEMAIL);
        SignupPhoneNumber.setText(newPHONE);
        SignupPassword.setText(newPASSWORD);
    } else {
        Toast.makeText(signuppage.this, "Text Field cannot be empty.", Toast.LENGTH_SHORT).show();
    }
}

}

java android sqlite android-sqlite
3个回答
1
投票

首先,删除数据库/表/列名称的变量定义中的前导和尾随空格:

public static final String DATABASE_NAME = "mylist.db";
public static final String TABLE_NAME = "mylist";
public static final String ID = "ID"; // I added this definition
public static final String USER_NAME = "username";
public static final String EMAIL = "email";
public static final String PHONE_NUMBER = "phone";
public static final String PASSWORD = "password";

然后,修复表的

CREATE
语句,其中不是定义列的数据类型,而是使用列名称两次,并且还错过了中间的逗号:

createTable = "CREATE TABLE " + TABLE_NAME + "(" +
              ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
              USER_NAME + " TEXT," +
              EMAIL + " TEXT," +
              PHONE_NUMBER + " TEXT," +
              PASSWORD + " TEXT)";

进行这些更改后,您必须从设备中卸载应用程序,以便删除数据库并重新运行以重新创建数据库和表。


1
投票
  1. 首先,数据库名中不需要写.db。

  2. 在onCreate()中这样写

    String query = "Create table if not exists " + TABLE_NAME + " (UESR_NAME, EMAIL, PHONE_NUMBER,PASSWORD)";
    db.execSQL(query);
    

  3. 创建一个在表中插入数据的方法,如下所示:

    
    public void insertData(String userName, String email, String phoneNumber, String password)
    {
    SQLiteDatabase db = getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("USER_NAME", userName);
    values.put("EMAIL", email);
    values.put("PHONE_NUMBER",phoneNumber);
    values.put("PASSWORD",password);
    db.insert(TABLE_NAME,null,values);
    db.close();
    }
    


0
投票

欲了解更多解决方案,您可以访问此链接,希望您得到您想要的解决方案。

点击解决方案

我知道你为什么来这里。你们都需要一个解决方案。如果您想解决所有学校数据库问题。您可以遵循我给出的指南。我是 YouTube 视频播放列表

© www.soinside.com 2019 - 2024. All rights reserved.