将用户信息发送到 phpMyAdmin 并在 App 中显示
Posted
技术标签:
【中文标题】将用户信息发送到 phpMyAdmin 并在 App 中显示【英文标题】:Sending User Information to phpMyAdmin and Displaying it in App 【发布时间】:2018-04-22 07:33:54 【问题描述】:所以在我的应用程序中,我有一个用户注册表单。在此用户注册表单中,有一个字段询问用户的家庭住址,我希望能够将用户在该字段中输入的任何地址发送到我在 Db 中创建的列中。我使用SQLiteHandler
。一些代码是外包的,所以在下面的代码中有可以工作的函数,但我不知道如何以及在哪里为我添加的这个新字段复制它。
这就是我所做的,由于某种原因,应用程序在尝试登录以注册新用户时不断崩溃。我将其缩小到SQLiteHandler.java
类,因为当我注释掉我所做的代码时,它可以让我使用应用程序的其余部分。在LogCat
中,堆栈跟踪表明user
表中没有名为patient_address
的列,但存在。
很抱歉发布了这么多代码,但我真的不知道从哪里开始或从哪里结束。我感谢任何可能知道从哪里开始的人的积极意见!
SQLiteHandler 代码: FULL CODE IS HERE
静态变量:
// Login Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_FIRSTNAME = "fristname";
private static final String KEY_LASTNAME = "lastname";
private static final String KEY_EMAIL = "email";
private static final String KEY_RANK = "rank";
private static final String KEY_UID = "uid";
private static final String KEY_HCP_ID = "HCP_id";
private static final String KEY_ENCRYPTEDPASSWORD = "encrypted_password";
private static final String KEY_SALT = "salt";
private static final String KEY_PASSWORD = "password";
private static final String KEY_TITLE = "tilte";
private static final String KEY_HOSPITAL = "hospital";
private static final String KEY_PROFESSION = "profession";
private static final String KEY_PHONE_NUMBER = "phone_number";
private static final String KEY_PATIENT_NUMBER = "patient_number";
private static final String KEY_ADDRESS = "patient_address";
创建数据库 OnCreate:
@Override
public void onCreate(SQLiteDatabase db)
String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_USER + "("
+ KEY_ID + " INTEGER PRIMARY KEY,"
+ KEY_FIRSTNAME + " TEXT,"
+ KEY_LASTNAME + " TEXT,"
+ KEY_TITLE + " TEXT,"
+ KEY_HOSPITAL + " TEXT,"
+ KEY_PROFESSION + " TEXT,"
+ KEY_RANK + " INT,"
+ KEY_EMAIL + " TEXT UNIQUE,"
+ KEY_UID + " TEXT,"
+ KEY_PASSWORD + "TEXT,"
+ KEY_ENCRYPTEDPASSWORD + " TEXT,"
+ KEY_SALT + "TEXT,"
+ KEY_PHONE_NUMBER + " INT,"
+ KEY_HCP_ID + " TEXT,"
+ KEY_PATIENT_NUMBER+ " TEXT,"
+ KEY_ADDRESS + "TEXT UNIQUE"
+")";
添加用户无效:
public void addUser(User user)
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_FIRSTNAME, user.getFirstname()); // FirstName
values.put(KEY_LASTNAME, user.getLastname()); // LastName
values.put(KEY_EMAIL, user.getEmail()); // Email
values.put(KEY_UID, user.getUnique_id()); // Email
values.put(KEY_RANK, user.getRank()); // rank
values.put(KEY_ENCRYPTEDPASSWORD, user.getEncrypted_password()); // Patient Of the HCP with this uid
values.put(KEY_SALT, user.getSalt()); // Patient Of the HCP with this uid
values.put(KEY_PASSWORD, user.getPassword()); // Password
values.put(KEY_TITLE, user.getTitle()); // HCP name
values.put(KEY_HOSPITAL, user.getHospital()); // HCP name
values.put(KEY_PROFESSION, user.getProfession()); // HCP name
values.put(KEY_HCP_ID, user.getHCP_id()); // Patient Of the HCP with this uid
values.put(KEY_PHONE_NUMBER, user.getPhone_number()); // phone number
values.put(KEY_PATIENT_NUMBER, user.getPatient_number()); // patient number
values.put(KEY_ADDRESS, user.getAddress()); // Patient Home Address
// Inserting Row
//long id = db.replace(TABLE_USER, null, values);
long id = db.insertWithOnConflict(TABLE_USER, null, values, SQLiteDatabase.CONFLICT_REPLACE);
db.close(); // Closing database connection
用户 getUserDetails():
public User getUsersDetails()
User user=new User();
String selectQuery = "SELECT * FROM " + TABLE_USER;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if (cursor.getCount() > 0)
user.setFirstname(cursor.getString(1));
user.setLastname(cursor.getString(2));
user.setTitle(cursor.getString(3));
user.setHospital(cursor.getString(4));
user.setProfession(cursor.getString(5));
user.setRank(Integer.valueOf(cursor.getString(6)));
user.setEmail(cursor.getString(7));
user.setUnique_id(cursor.getString(8));
user.setPassword(cursor.getString(9));
user.setEncrypted_password(cursor.getString(10));
user.setSalt(cursor.getString(11));
user.setPhone_number(Long.valueOf(cursor.getString(12)));
user.setHCP_id(cursor.getString(13));
user.setPatient_number(cursor.getString(14));
user.setAddress(cursor.getString(15));
else
user=null;
cursor.close();
db.close();
// return user
return user;
通过电子邮件获取用户详细信息:
public User getUserDetailsByEmail(String email)
User user=new User();
String selectQuery = "SELECT * FROM " + TABLE_USER + " WHERE "+ KEY_EMAIL + " = '"+ email+"'";
Log.d("tables select", selectQuery);
SQLiteDatabase db = this.getReadableDatabase();
Log.d("tables",tableToString(db,TABLE_USER));
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if (cursor.getCount() > 0)
user.setFirstname(cursor.getString(1));
user.setLastname(cursor.getString(2));
user.setTitle(cursor.getString(3));
user.setHospital(cursor.getString(4));
user.setProfession(cursor.getString(5));
user.setRank(Integer.valueOf(cursor.getString(6)));
user.setEmail(cursor.getString(7));
user.setUnique_id(cursor.getString(8));
user.setPassword(cursor.getString(9));
user.setEncrypted_password(cursor.getString(10));
user.setSalt(cursor.getString(11));
user.setPhone_number(Long.valueOf(cursor.getString(12)));
user.setHCP_id(cursor.getString(13));
user.setPatient_number(cursor.getString(14));
user.setAddress(cursor.getString(15));
else
user=null;
cursor.close();
db.close();
// return user
return user;
通过找出谁注册来获取用户的详细信息:
public ArrayList<User> getUsersDetailsByHCP_id(String HCP_id)
User user;
String selectQuery = "SELECT * FROM " + TABLE_USER + " WHERE "+ KEY_HCP_ID + " = '"+ HCP_id+"'";
Log.d("tables select", selectQuery);
SQLiteDatabase db = this.getReadableDatabase();
Log.d("tables",tableToString(db,TABLE_USER));
Cursor cursor = db.rawQuery(selectQuery, null);
ArrayList<User> list = new ArrayList<>();
// Move to first row
cursor.moveToFirst();
if (cursor.getCount() > 0) do
user = new User();
user.setFirstname(cursor.getString(1));
user.setLastname(cursor.getString(2));
user.setTitle(cursor.getString(3));
user.setHospital(cursor.getString(4));
user.setProfession(cursor.getString(5));
user.setRank(Integer.valueOf(cursor.getString(6)));
user.setEmail(cursor.getString(7));
user.setUnique_id(cursor.getString(8));
user.setPassword(cursor.getString(9));
user.setEncrypted_password(cursor.getString(10));
user.setSalt(cursor.getString(11));
user.setPhone_number(Long.valueOf(cursor.getString(12)));
user.setHCP_id(cursor.getString(13));
user.setPatient_number(cursor.getString(14));
user.setAddress(cursor.getString(15));
list.add(user);
while (cursor.moveToNext());
else
Log.i (TAG,"GetUsersDetailByHCP_id no Users found");
cursor.close();
db.close();
// return user
return list;
来自 LogCat 的堆栈跟踪:
0:45:28.288 27837-27837/? E/SQLiteLog: (1) table user has no column named patient_address
11-09 10:45:28.293 27837-27837/? E/androidRuntime: FATAL EXCEPTION: main
Process: co.uk.mednet.mednet_ghana, PID: 27837
android.database.sqlite.SQLiteException: table user has no column named patient_address (code 1): , while compiling: INSERT OR REPLACE INTO user(encrypted_password,profession,password,fristname,patient_number,patient_address,hospital,HCP_id,tilte,rank,lastname,email,uid,salt,phone_number) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(table user has no column named patient_address (code 1): , while compiling: INSERT OR REPLACE INTO user(encrypted_password,profession,password,fristname,patient_number,patient_address,hospital,HCP_id,tilte,rank,lastname,email,uid,salt,phone_number) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?))
#################################################################
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1008)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:573)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1864)
at co.uk.mednet.mednet_ghana.helper.SQLiteHandler.addUser(SQLiteHandler.java:181)
at co.uk.mednet.mednet_ghana.activity.LoginActivity$3.onResponse(LoginActivity.java:180)
at co.uk.mednet.mednet_ghana.activity.LoginActivity$3.onResponse(LoginActivity.java:163)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
数据库结构:
@Override
public void onCreate(SQLiteDatabase db)
String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_USER + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_FIRSTNAME + " TEXT," + KEY_LASTNAME + " TEXT," + KEY_TITLE + " TEXT," + KEY_HOSPITAL + " TEXT,"+ KEY_PROFESSION + " TEXT,"+ KEY_RANK + " INT,"
+ KEY_EMAIL + " TEXT UNIQUE," + KEY_UID + " TEXT," + KEY_PASSWORD + " TEXT," + KEY_ENCRYPTEDPASSWORD + " TEXT," + KEY_SALT + " TEXT,"+ KEY_PHONE_NUMBER + " INT," + KEY_HCP_ID + " TEXT,"
+ KEY_PATIENT_NUMBER+ " TEXT," + KEY_ADDRESS + "TEXT" + ")";
db.execSQL(CREATE_LOGIN_TABLE);
【问题讨论】:
您的日志显示table user has no column named patient_address
。你确定你的数据库创建成功了吗?
in my application I have a user registration form
。什么样的?它从何而来?你如何显示它?不知道你在说什么。
这跟phpadmin和mysql有什么关系?
数据库在 phpMyAdmin 中。我删除了mysql
标签。注册表单纯粹是一个 EditText 和一个 TextView 表单。 @MohammadJulfikar 我检查了数据库,patient_address
列已成功创建,我也正确引用了它。
@DezG 你的日志猫说table user has no column named patient_address
。这意味着您的 sqlite 绝对有问题。我提供了一个示例代码。你可以检查
【参考方案1】:
我将提供我自己的工作代码。稍后你可以修改它。
public class EmployeeDB extends SQLiteOpenHelper
public EmployeeDB(Context context)
super(context,ETBL_NAME,null,1);
@Override
public void onCreate(SQLiteDatabase db)
String createTable = "CREATE TABLE "+ ETBL_NAME + "( ID INTEGER PRIMARY KEY AUTOINCREMENT, "+
COLUMN_NAME + " TEXT, " +
COLUMN_ID + " TEXT, " +
COLUMN_PROGRAMME + " TEXT, " +
COLUMN_EMAIL + " TEXT, " +
COLUMN_MOBILE + " TEXT, " +
COLUMN_WORKING + " TEXT)";
db.execSQL(createTable);
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
onCreate(db);
public boolean AddInfo(String name,String id,String programme,String email,String mobile,String working)
boolean flag;
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME,name);
values.put(COLUMN_ID,id);
values.put(COLUMN_EMAIL,email);
values.put(COLUMN_MOBILE,mobile);
values.put(COLUMN_PROGRAMME,programme);
values.put(COLUMN_WORKING,working);
long result = database.insert(ETBL_NAME,null,values);
flag = result != -1;
return flag;
public boolean DeleteAll()
SQLiteDatabase database = this.getReadableDatabase();
int affected = database.delete(ETBL_NAME,null,null);
return affected > 0;
public Cursor GetAllData()
SQLiteDatabase database = this.getWritableDatabase();
String query = "SELECT * FROM " + ETBL_NAME;
return database.rawQuery(query,null);
public boolean updateDetails(String matri,String row, String content)
SQLiteDatabase database = this.getReadableDatabase();
ContentValues args = new ContentValues();
args.put(row, content);
int i = database.update(ETBL_NAME, args, COLUMN_ID + "='" + matri +"'", null);
return i > 0;
我从您的代码中假设是,您的数据库没有成功创建。可能是您忘记在 oncreate 方法中执行 SQL。你的插入部分看起来不错。你可以关注我的功能。他们都在工作
【讨论】:
但令人困惑的是,在我添加到这个新的patient_address
列之前它运行良好
ow.. 你应该早点告诉这个。在这种情况下,您必须卸载应用程序并重新安装.. 否则 sqlite 将不会更新
我在 android studio 中使用 Run 功能,所以应用程序会自动卸载。我还是试过了,还是崩溃了
你想让我给你看我的数据库以便你检查我是否正确插入了列吗?可能需要给我发消息,这样我才不会弄乱这篇文章
@DezG 我正要问这个。你能编辑你的问题并分享完整的代码吗?以上是关于将用户信息发送到 phpMyAdmin 并在 App 中显示的主要内容,如果未能解决你的问题,请参考以下文章
如何将用户的位置发送和接收到数据库并在地图中使用它[Android]