gpt4 book ai didi

java - getDatabase 递归调用 sqlite insert 和 getReadableDatabase()

转载 作者:搜寻专家 更新时间:2023-11-01 07:50:42 27 4
gpt4 key购买 nike

我刚开始使用我的第一个应用程序,我正在尝试创建一个表格设置,其中应该包含一个输入 PIN。之后我想运行方法 getPIN() 来获取此设置的值。

问题是,我总是得到错误“递归调用 getDatabase”。因为我刚开始,所以我不知道如何解决它。我见过很多人曾经遇到过同样的问题,但解决方案对我不起作用。你能帮我理解我做错了什么吗。

联系人表是我之前遵循的教程的一部分,这就是它仍然存在的原因。

谢谢。

这是我的数据库助手

public class DatabaseHelper extends SQLiteOpenHelper {

private static final int DATABASE_VERSION =3;
private static final String DATABASE_NAME = "contacts.db";
private static final String TABLE_NAME = " contacts";
private static final String COLUMN_ID = "id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_EMAIL = "email";
private static final String COLUMN_PASS = "pass";
SQLiteDatabase db;
private static final String TABLE_CREATE = "create table contacts (id integer primary key not null ," +
"name text not null, email text not null, pass text not null);";
private static final String TABLE_SETTINGS = "create table settings (id integer primary key not null ," +
"setting text not null, value integer not null);";





public DatabaseHelper(Context context){
super(context, DATABASE_NAME, null,DATABASE_VERSION );
}
@Override
public void onCreate(SQLiteDatabase db){
String ssetting = "PIN";
db.execSQL(TABLE_CREATE);
db.execSQL(TABLE_SETTINGS);

db=this.getWritableDatabase();
ContentValues values = new ContentValues();
String query = "Select * from settings";
Cursor cursor = db.rawQuery(query, null);
int count = cursor.getCount();

values.put("id",count);
values.put("setting", ssetting);
values.put("value", 0);

db.insert("settings", null, values);
db.close();

this.db=db;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
String query = "DROP TABLE IF EXISTS"+TABLE_NAME;
db.execSQL(query);
this.onCreate(db);
}
public int insertContact(Contact c){
db=this.getWritableDatabase();
ContentValues values = new ContentValues();
String query = "Select * from contacts";
Cursor cursor = db.rawQuery(query, null);
int count = cursor.getCount();

values.put(COLUMN_ID,count);
values.put(COLUMN_NAME, c.getName());
values.put(COLUMN_EMAIL, c.getEmail());
values.put(COLUMN_PASS, c.getPass());

db.insert(TABLE_NAME, null, values);
db.close();
return 0;
}
public String searchPass(String uname)
{
db = this.getReadableDatabase();
String query = "select name, pass from "+TABLE_NAME;
Cursor cursor = db.rawQuery(query, null);
String a,b;
b="not found";
if (cursor.moveToFirst()){
do {
a = cursor.getString(0);
b = cursor.getString(1);

if (a.equals(uname)) {
b = cursor.getString(1);
break;
}
}
while(cursor.moveToNext());
}
db.close();
return b;
}
public int getPIN()
{
db = this.getReadableDatabase();
int pin=1;
String query = "select id, setting, value from settings where setting='PIN'";
Cursor cursor = db.rawQuery(query, null);

if (cursor.moveToFirst()){
do {
pin = cursor.getInt(2);
break;
}
while(cursor.moveToNext());
}
db.close();
return pin;
}

和 MainActivity

public class MainActivity extends AppCompatActivity {
DatabaseHelper helper = new DatabaseHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int pin = helper.getPIN();
if (pin==0){
Intent i = new Intent(MainActivity.this, Loginpin.class);
startActivity(i);
}
if (pin==1) {
Intent i = new Intent(MainActivity.this, Setuppin.class);
startActivity(i);
}
//pin = helper.getPIN();
}
public void b_newOnClick(View v) {
if (v.getId() == R.id.blogin)
{
EditText user = (EditText) findViewById(R.id.tfuser);
String su = user.getText().toString();
EditText pass = (EditText) findViewById(R.id.tf_pw);
String spass = pass.getText().toString();

String password = helper.searchPass(spass);
if(spass.equals(password))
{
Intent i = new Intent(MainActivity.this, Welcome.class);
i.putExtra("username", su);
startActivity(i);
}
else{
Toast.makeText(MainActivity.this, "Password incorrect", Toast.LENGTH_LONG).show();
}
}


}
public void onSignClick(View v) {
if (v.getId() == R.id.bsign) {
Intent i = new Intent(MainActivity.this, SignUp.class);
startActivity(i);

}
}

非常感谢。

问候,S乞力马沙罗

最佳答案

不要调用 getWritableDatabase()getReadableDatabase()来自你的 SQLiteOpenHelper生命周期方法,例如 onCreate()onUpgrade()依次调用 get...Database() 触发.相反,使用 SQLiteDatabase作为这些方法的参数给出的对象。

具体来说,从您的 onCreate() 中删除这一行:

db=this.getWritableDatabase();

关于java - getDatabase 递归调用 sqlite insert 和 getReadableDatabase(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35549041/

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