gpt4 book ai didi

java - Android 应用程序 SQLiteDatabase 插入新值不起作用,代码中有 0 个错误

转载 作者:行者123 更新时间:2023-11-29 00:21:41 27 4
gpt4 key购买 nike

我正在制作一个 Android 应用程序,我从 androidhive 获得了一些已完成的解决方案,它有一个数据库等,所以我试图向该数据库添加更多值,例如地址、电话等,但每当我在代码,应用程序无法运行...

例如,现在我只想从数据库中读取值来显示我的帐户信息(我从我的网站注册表格中插入的,帐户信息在那里工作得很好)

用于读取信息的我的数据库处理程序类:

// Database Name
private static final String DATABASE_NAME = "login";
// Login table name
private static final String TABLE_LOGIN = "login";


public HashMap<String, String> getUserDetails(){
HashMap<String,String> user = new HashMap<String,String>();
String selectQuery = "SELECT * FROM " + TABLE_LOGIN;

SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if(cursor.getCount() > 0){
user.put("fname", cursor.getString(1));
user.put("lname", cursor.getString(2));
user.put("email", cursor.getString(3));
user.put("uname", cursor.getString(4));
user.put("uid", cursor.getString(5));
user.put("created_at", cursor.getString(6));
/*user.put("address", cursor.getString(7));
user.put("tel", cursor.getString(8));
user.put("ptc", cursor.getString(9));
user.put("dob", cursor.getString(10));*/
}
cursor.close();
db.close();
// return user
return user;
}

如您所见,这个注释部分是我想添加到应用程序中的内容,因此它会在应用程序中读取这些值。这是我的个人资料.java

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);


DatabaseHandler db = new DatabaseHandler(getApplicationContext());

HashMap<String,String> user = new HashMap<String, String>();
user = db.getUserDetails();

/**
* Displays the account details in Text view
**/

final TextView fname = (TextView)findViewById(R.id.fname);
final TextView lname = (TextView)findViewById(R.id.lname);
final TextView uname = (TextView)findViewById(R.id.uname);
final TextView email = (TextView)findViewById(R.id.email);
final TextView address = (TextView)findViewById(R.id.address);
final TextView tel = (TextView)findViewById(R.id.tel);
final TextView ptc = (TextView)findViewById(R.id.ptc);
final TextView dob = (TextView)findViewById(R.id.dob);
final TextView created_at = (TextView)findViewById(R.id.regat);
fname.setText(user.get("fname"));
lname.setText(user.get("lname"));
uname.setText(user.get("uname"));
email.setText(user.get("email"));
address.setText(user.get("address"));
tel.setText(user.get("tel"));
ptc.setText(user.get("ptc"));
dob.setText(user.get("dob"));
created_at.setText(user.get("created_at")); }}

这是我的数据库:

CREATE TABLE `login`.`member` (   
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`username` VARCHAR(30) NOT NULL,
`encrypted_password` VARCHAR(80) NOT NULL,
`email` VARCHAR(50) NOT NULL,
`fname` VARCHAR(30) NOT NULL,
`lname` VARCHAR(30) NOT NULL,
`dob` VARCHAR(10) NOT NULL,
`tel` VARCHAR(15) NOT NULL,
`address` VARCHAR(50) NOT NULL,
`ptc` VARCHAR(10) NOT NULL,
`salt` VARCHAR(10) NOT NULL,
`created_at` datetime DEFAULT NULL
) ENGINE = InnoDB;

请记住,它在网站上运行得非常好,顺便说一句,当我尝试将这些额外的字符串添加到应用程序的注册中时,它会发生同样的情况(应用程序停止工作)并且当我不添加任何新内容时应用程序代码,只有当我像现在这样保留它时(带有注释的添加部分)它才能完美运行并读取名字,姓氏,用户名,电子邮件,创建于。

这里可能有什么问题?是数据库吗? .java 代码?我的 API?

最佳答案

您的数据库是login,您指的是一个名为login 的表。

String selectQuery = "SELECT  * FROM " + TABLE_LOGIN; // table name is 'login'

但是您在 login 数据库中创建了一个名为 member 的表。

CREATE TABLE `login`.`member`

显然它不应该工作。

即使您有打字错误,根据您的 select 语句,您的 cursor.getString 序列也是错误的。

当您通过指定 * 选择所有 字段时,您必须遵守要在 getString 方法中使用的列的顺序编号.

如果我没记错的话,他们应该是:

user.put("uid", cursor.getString(1)); // id - uid
user.put("username", cursor.getString(2)); // uname - username
user.put("email", cursor.getString(4));
user.put("fname", cursor.getString(5));
user.put("lname", cursor.getString(6));
user.put("dob", cursor.getString(7));
user.put("tel", cursor.getString(8));
user.put("address", cursor.getString(9));
user.put("ptc", cursor.getString(10));
user.put("created_at", cursor.getString(12));

关于java - Android 应用程序 SQLiteDatabase 插入新值不起作用,代码中有 0 个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20800385/

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