我使用SQLite数据库来保存用户信息,例如用户名密码年龄性别高度等
数据库似乎工作正常(调试时没有错误或异常)但是当我去检索单个用户和他/她的详细信息时,应用程序只是在单击按钮后停止。
我有一个databasehandler java文件,其中包含用于单个用户检索的方法和一个带有setter和getter方法的Registerdetails文件。
该按钮的onclicklistner设置为名为Userinfo()
的方法。
用户界面有一个edittext框用于输入id,一个按钮和一个textview来显示结果
我的问题是我的查询错误或是我的数据库?
RegisterDetails.java
public class Registerdetails {
int _id;
String _user_name;
String _password;
String _gender;
double _height;
int _age;
public Registerdetails(){}
public Registerdetails(int _id, String _user_name,String _password, String _gender, double _height, int _age){
this._user_name = _user_name;
this._password = _password;
this._age = _age;
this._gender = _gender;
this._height = _height;
this._id = _id;
}
public int getID(){
return this._id;
}
public void setID(int id){
this._id = id;
}
public String get_user_name(){
return this._user_name;
}
public void set_user_name(String user_name){
this._user_name = user_name;
}
public String get_password(){
return this._password;
}
public void set_password(String password){
this._password = password;
}
public String get_gender(){
return this._gender;
}
public void set_gender(String gender){
this._user_name = gender;
}
public int get_age(){
return this._age;
}
public void set_age(int age){
this._age = age;
}
public double get_height(){
return this._height;
}
public void set_height(double height){
this._height = height;
}
}
DatabaseHandler.java
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.example.john.databasetest.Product;
import com.example.john.databasetest.Registerdetails;
public class DatabaseHandler extends SQLiteOpenHelper {
//DATABASE VERSION
private static final int DATABASE_VERSION = 1;
//DATABASE NAME
private static final String DATABASE_NAME = "Appdatabase";
//TABLE NAMES
private static final String TABLE_REGISTER = "Register_Details";
//REGISTER COLUMN NAMES
private static final String KEY_USERID = "UserID";
private static final String KEY_USERNAME = "Username";
private static final String KEY_PASSWORD = "Password";
private static final String KEY_AGE = "Age";
private static final String KEY_GENDER = "Gender";
private static final String KEY_HEIGHT = "Height";
public DatabaseHandler(Context context){
super(context,DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db){
String CREATE_REGISTERDETAILS_TABLE = "CREATE TABLE " + TABLE_REGISTER + " ( "+ KEY_USERID + " INTEGER PRIMARY KEY , " + KEY_USERNAME + " TEXT, " +
KEY_PASSWORD + " TEXT, " +
KEY_GENDER + " TEXT, " +
KEY_HEIGHT + " DOUBLE, " +
KEY_AGE + " INTEGER, " +
);";
//CREATING REQUIRED TABLES
db.execSQL(CREATE_REGISTERDETAILS_TABLE);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
//ON UPGRADE DROP THE OLDER TABLES
db.execSQL("DROP TABLE IF EXISTS" + TABLE_REGISTER);
onCreate(db);
}
//query for retrieving a single register
Registerdetails get_userinfo (int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_REGISTER, new String[] { KEY_USERID,
KEY_USERNAME, KEY_PASSWORD }, KEY_USERID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if(cursor != null)
cursor.moveToFirst();
Registerdetails user_info = new Registerdetails(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2),cursor.getString(3), cursor.getDouble(4),
cursor.getDouble(5));
cursor.close();
return user_info;
}
}
Home.java
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;
import java.util.List;
public class Home extends ActionBarActivity {
Button btn;
TextView dbtxt;
EditText idedit;
int id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
btn = (Button) findViewById(R.id.btndb);
dbtxt = (TextView) findViewById(R.id.txtdb);
DatabaseHandler db = new DatabaseHandler(this);
Log.d("Inserting: ", "Inserting ..");
db.addRegistered_user(new Registerdetails(0, "steve", "2345", "Male", 182.0, 21));
db.addRegistered_user(new Registerdetails(1, "kevin", "1234","Male", 186.0,20));
db.addRegistered_user(new Registerdetails(2, "paul", "5555","Male", 178.0, 86.0, 87.5, 1.5,20,0.0,0.0,0.0,0.0,0.0,0.0,0.0, 600.00));
}
public void Userinfo(){
idedit = (EditText) findViewById(R.id.IDedit);
id = Integer.parseInt(idedit.getText().toString());
DatabaseHandler db = new DatabaseHandler(this);
Registerdetails user = db.get_userinfo(id);
String Log = "Id: " + user.getID() + "\nUsername: " + user.get_user_name() + "Password: " + user.get_password();
dbtxt.setText(Log);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Main_activity.xml
<TextView
android:layout_width="300dp"
android:layout_height="80dp"
android:id="@+id/txtdb"
android:backgroundTint="#ff75ff2f"
android:text="Results"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textAlignment="center" />
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:text="PRESS"
android:id="@+id/btndb"
android:onClick="Viewuserinfo"
android:layout_below="@+id/txtdb"
android:layout_centerHorizontal="true"
android:layout_marginTop="41dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/IDedit"
android:layout_above="@+id/txtdb"
android:layout_centerHorizontal="true"
android:layout_marginBottom="82dp" />
答案 0 :(得分:1)
1 - 删除多余的逗号:
KEY_AGE + " INTEGER, " + );";
在创建表格时,您应该收到错误 如果没有,则表示您更改了表结构,但仍然看到旧表。
这是因为您没有通知您的应用程序数据库已更新。
事实上,你的
private static final int DATABASE_VERSION = 1;
仍 1 。
为了使onUpgrade()
方法触发,您必须增加该值
即,将其更改为 2 :
private static final int DATABASE_VERSION = 2;
2 - 您在此处插入了太多列值:
db.addRegistered_user(new Registerdetails(2, "paul", "5555","Male", 178.0, 86.0, 87.5, 1.5,20,0.0,0.0,0.0,0.0,0.0,0.0,0.0, 600.00));
您只能在表格中插入尽可能多的列。
答案 1 :(得分:0)
尝试重用db DatabaseHandler
public class Home extends ActionBarActivity {
Button btn;
TextView dbtxt;
EditText idedit;
int id;
DatabaseHandler db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
db = new DatabaseHandler(this);
btn = (Button) findViewById(R.id.btndb);
dbtxt = (TextView) findViewById(R.id.txtdb);
Log.d("Inserting: ", "Inserting ..");
db.addRegistered_user(new Registerdetails(0, "steve", "2345", "Male", 182.0, 21));
db.addRegistered_user(new Registerdetails(1, "kevin", "1234","Male", 186.0,20));
db.addRegistered_user(new Registerdetails(2, "paul", "5555","Male", 178.0, 86.0, 87.5, 1.5,20,0.0,0.0,0.0,0.0,0.0,0.0,0.0, 600.00));
}
public void Userinfo(){
idedit = (EditText) findViewById(R.id.IDedit);
id = Integer.parseInt(idedit.getText().toString());
// reuse the db object
Registerdetails user = db.get_userinfo(id);
String Log = "Id: " + user.getID() + "\nUsername: " + user.get_user_name() + "Password: " + user.get_password();
dbtxt.setText(Log);
}