gpt4 book ai didi

java - 使用 SQLite 创建表时出错

转载 作者:太空宇宙 更新时间:2023-11-04 12:16:55 26 4
gpt4 key购买 nike

我想创建 2 个表:一个是注册信息,第二个是个人信息。我创建了一个与两个表分开的数据库操作文件,但是当我在应用程序中运行它时,出现此错误:

 Info Database Created successfully
09-02 19:19:03.724 12697-12697/com.example.user.myapplication E/SQLiteLog: (1) no such table: personal_info
09-02 19:19:03.724 12697-12697/com.example.user.myapplication E/SQLiteDatabase: Error inserting cont=8755664455 addr=Sham Nagar reg_no=CS20161802
android.database.sqlite.SQLiteException: no such table: personal_info (code 1): , while compiling: INSERT INTO personal_info(cont,addr,reg_no) 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)
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:1469)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
at com.example.user.myapplication.PersonalInfoDataOp.personalInfoOp(PersonalInfoDataOp.java:51)
at com.example.user.myapplication.student_info$2.onClick(student_info.java:99)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
09-02 19:19:03.724 12697-12697/com.example.user.myapplication D/Database operations: Info One row inserted

这是表数据类。我在这里提到了所有表属性。

public TableData(){
}

public static abstract class TableInfo implements BaseColumns{
public static final String FIRST_NAME = "first_name" ;
public static final String LAST_NAME = "last_name" ;
public static final String SIT_NO = "sit_number" ;
public static final String REG_NO = "reg_number" ;
public static final String DATABASE_NAME = "student_database" ;
public static final String TABLE_NAME = "registration_info" ;
}

public static abstract class PersonalInfo implements BaseColumns{
public static final String REGISTRATION_NO = "reg_no";
public static final String ADDRESS = "addr";
public static final String CONTACT = "cont";
public static final String DATABASE_NAME = "student_database";
public static final String TABLE_NAME = "personal_info";
}

这里我创建了注册信息的数据库操作类

public class DatabaseOperations extends SQLiteOpenHelper {

public static final int database_version = 1;

public String CREATE_QUERY = "CREATE TABLE " + TableData.TableInfo.TABLE_NAME+ "("
+ TableData.TableInfo.FIRST_NAME+ " TEXT ," + TableData.TableInfo.LAST_NAME + " TEXT ,"
+ TableData.TableInfo.SIT_NO + " TEXT ," + TableData.TableInfo.REG_NO + " TEXT);";

public DatabaseOperations(Context context) {
super(context, TableData.TableInfo.DATABASE_NAME, null, database_version);
Log.d("Database operations", "Reg Database Created successfully");
}

@Override
public void onCreate(SQLiteDatabase sdb) {

sdb.execSQL(CREATE_QUERY);
Log.d("Database operations", "Reg Table Created successfully");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

public void regInfoOperation(DatabaseOperations dop, String First_Name, String Last_Name, String Sit_Number, String Reg_Number)
{
SQLiteDatabase RSQ = dop.getWritableDatabase();
ContentValues rcv = new ContentValues();

rcv.put(TableData.TableInfo.FIRST_NAME,First_Name);
rcv.put(TableData.TableInfo.LAST_NAME,Last_Name);
rcv.put(TableData.TableInfo.SIT_NO,Sit_Number);
rcv.put(TableData.TableInfo.REG_NO,Reg_Number);

long k = RSQ.insert(TableData.TableInfo.TABLE_NAME, null, rcv);
Log.d("Database operations", "Reg One row inserted");
}

public Cursor getRegInformation(DatabaseOperations dop, String regNo)
{
SQLiteDatabase db = dop.getReadableDatabase();
String[] coloumns = {TableData.TableInfo.FIRST_NAME, TableData.TableInfo.LAST_NAME, TableData.TableInfo.REG_NO};
String where = TableData.TableInfo.REG_NO +" LIKE ?";
String args[] = {regNo};
Cursor CR = db.query(TableData.TableInfo.TABLE_NAME,coloumns, where, args, null, null, null);
return CR;
}
}

这里Personal是PersonalInfoOp数据库的操作类

public class PersonalInfoDataOp extends SQLiteOpenHelper {

public static final int database_version = 1;

public String CREATE_QUERY = "CREATE TABLE " + TableData.PersonalInfo.TABLE_NAME + "( "
+ TableData.PersonalInfo.REGISTRATION_NO + " TEXT,"
+ TableData.PersonalInfo.ADDRESS + " TEXT,"
+ TableData.PersonalInfo.CONTACT + " TEXT);";

public PersonalInfoDataOp(Context context) {
super(context, TableData.PersonalInfo.DATABASE_NAME, null, database_version);
Log.d("Database operations", " Info Database Created successfully");
}

@Override
public void onCreate(SQLiteDatabase dob) {
try {
dob.execSQL(CREATE_QUERY);
Log.d("Database operations", "Personal info Table Created successfully");
}
catch (Exception e){
Log.d("Database operations","Personal info Fail to create");
}
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

public void personalInfoOp(PersonalInfoDataOp dop, String Registration_no, String Addr, String Cont)
{
SQLiteDatabase PSQ = dop.getWritableDatabase();
ContentValues pcv = new ContentValues();

pcv.put(TableData.PersonalInfo.REGISTRATION_NO, Registration_no);
pcv.put(TableData.PersonalInfo.ADDRESS,Addr);
pcv.put(TableData.PersonalInfo.CONTACT, Cont);

long k = PSQ.insert(TableData.PersonalInfo.TABLE_NAME, null, pcv);
Log.d("Database operations", "Info One row inserted");
}
}

这是学生信息类。当我单击下一步按钮时,logCaT 中出现错误。

Next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1 = new Intent(student_info.this,info.class);
add = Address.getText().toString();
con = Contact.getText().toString();

PersonalInfoDataOp POP = new PersonalInfoDataOp(popc);
POP.personalInfoOp(POP,RegistrationNum,add,con);

startActivity(intent1);
Toast.makeText(getBaseContext(),"Personal Info inserted......", Toast.LENGTH_LONG).show();

}
});

最佳答案

您的两个 SQLiteOpenHelper 具有相同的数据库文件名:student_databaseSQLiteOpenHelper 版本是数据库文件,而不是表,因此在第二个帮助程序中不会执行任何操作,因为数据库文件已经处于正确的版本。

要修复它,

  • 将两个表移至同一个 SQLiteOpenHelper
  • 删除onCreate()中的try-catch。错误时需要抛出异常。
  • 卸载您的应用以删除旧的数据库文件并让 onCreate() 再次执行。

参见When is SQLiteOpenHelper onCreate() / onUpgrade() run?了解更多。

关于java - 使用 SQLite 创建表时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39294233/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com