gpt4 book ai didi

java - 尝试将值插入 sqlite 数据库表时应用程序崩溃

转载 作者:行者123 更新时间:2023-12-01 21:23:43 25 4
gpt4 key购买 nike

我创建了一个包含几个表的数据库。当我调用 Database.java 时,数据库被创建(应用程序不会崩溃)。

当我尝试插入登录表时,应用程序崩溃了。我不知道下一步该怎么做。我认为问题出在表插入代码上。

建表代码(Database.java)

public static final String db="shg.db";
public static final int version=1;

public Database(Context context) {
super(context, db, null, version);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table if not exists login(id integer primary key autoincrement,log_name text,log_pass text)");
db.execSQL("create table if not exists members(foreign key (id) REFERENCES login(id),mem_name text,mem_dob date,mem_address text,mem_phno text)");
db.execSQL("create table if not exists committee(foreign key (id) REFERENCES login(id),committee_id int autoincrement,committee_date date)");
db.execSQL("create table if not exists status(foreign key (id) REFERENCES login(id),loan_count int,thrift_complete int)");
db.execSQL("create table if not exists attendance(foreign key (committee_id) REFERENCES login(committee_id),foreign key (id) REFERENCES login(id))");
db.execSQL("create table if not exists loan(loan_id int,foreign key (committee_id) REFERENCES login(committee_id),foreign key (id) REFERENCES login(id),amount numeric)");
db.execSQL("create table if not exists loanrepayment(foreign key (id) REFERENCES login(id),foreign key (loan_id) REFERENCES login(loan_id),foreign key(committee_id) REFERENCES login(committee_id),amount numeric,interest numeric)");
db.execSQL("create table if not exists thrift(foreign key (committee_id) REFERENCES login(committee_id),foreign key (id) REFERENCES login(id),amount numeric)");
db.execSQL("create table if not exists fine(foreign key (committee_id) REFERENCES login(committee_id),foreign key (id) REFERENCES login(id),amount numeric,reason text)");
}

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

public boolean insertlogin(String name,String pass) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("log_name",name);
cv.put("log_pass",pass);
db.insert("login",null,cv);
return true;
}
public Cursor getlogin(String uname,String upass) {
Cursor c;
String qry="select * from login where log_name ="+uname+" and log_pass =" +upass+";";
SQLiteDatabase db = this.getReadableDatabase();
String[]col_name={"log_name","log_pass"};
String whereClause = "log_name = ? OR log_pass = ?";
String[] whereArgs = new String[] {uname, upass};
c=db.query("login",col_name,whereClause,whereArgs,null,null,null);
return c;
}

数据库插入代码(MainActivity.java)

db=new Database(this);
Boolean status=db.insertlogin("a","b");
Toast.makeText(MainActivity.this, ""+status, Toast.LENGTH_SHORT).show();

不知何故,我设法在我在开发项目阶段使用的设备上运行该应用程序(没有崩溃)。当我尝试在新设备上运行该项目时,应用程序崩溃了。

E/SQLiteLog: (1) near "foreign": syntax error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: droid.dev.shg.shg, PID: 29705
java.lang.RuntimeException: Unable to start activity ComponentInfo{droid.dev.shg.shg/droid.dev.shg.shg.MainActivity}: android.database.sqlite.SQLiteException: near "foreign": syntax error (code 1): , while compiling: create table if not exists members(foreign key (id) REFERENCES login(id),mem_name text,mem_dob date,mem_address text,mem_phno text)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2584)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2666)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4336)
at android.app.ActivityThread.-wrap15(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1499)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5769)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
Caused by: android.database.sqlite.SQLiteException: near "foreign": syntax error (code 1): , while compiling: create table if not exists members(foreign key (id) REFERENCES login(id),mem_name text,mem_dob date,mem_address text,mem_phno text)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:896)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:507)
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.executeSql(SQLiteDatabase.java:1704)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1635)
at droid.dev.shg.shg.Database.onCreate(Database.java:22)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at droid.dev.shg.shg.Database.insertlogin(Database.java:39)
at droid.dev.shg.shg.MainActivity$override.onCreate(MainActivity.java:30)
at droid.dev.shg.shg.MainActivity$override.access$dispatch(MainActivity.java)
at droid.dev.shg.shg.MainActivity.onCreate(MainActivity.java:0)
at android.app.Activity.performCreate(Activity.java:6583)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1114)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2531)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2666) 
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4336) 
at android.app.ActivityThread.-wrap15(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1499) 
at android.os.Handler.dispatchMessage(Handler.java:111) 
at android.os.Looper.loop(Looper.java:207) 
at android.app.ActivityThread.main(ActivityThread.java:5769) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

最佳答案

您的“外键”需要以正常方式声明,然后指定为外键。例如:

create table members(id INTEGER, mem_name TEXT, FOREIGN KEY(id) REFERENCE login(id))

关于java - 尝试将值插入 sqlite 数据库表时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38685085/

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