我想创建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
这是Table Data类。我在这里提到了所有的表属性。
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;
}
}
此处为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();
}
});
答案 0 :(得分:0)
您的SQLiteOpenHelper
都具有相同的数据库文件名:student_database
。 SQLiteOpenHelper
版本数据库文件,而不是表,因此在第二个帮助程序中没有任何操作,因为数据库文件已经是正确的版本。
要修复它,
SQLiteOpenHelper
try
中的catch
- onCreate()
。例外情况需要在错误时抛出。onCreate()
再次执行。有关详情,请参阅When is SQLiteOpenHelper onCreate() / onUpgrade() run?。