更改資料庫連接,testdb->care

This commit is contained in:
kuei
2024-09-04 08:26:22 +08:00
parent 10d856208d
commit 968ef745a2

View File

@@ -18,7 +18,7 @@ class PersonalInfo extends StatefulWidget {
} }
class _PersonalInfoState extends State<PersonalInfo> { class _PersonalInfoState extends State<PersonalInfo> {
String _name = '', _phone = '', _age = '', _gender = '', _email = ''; String _name = '', _phone = '', _gender = '', _address='', _email = '', _password = '';
bool _isEditing = false; //是否為編輯狀態 bool _isEditing = false; //是否為編輯狀態
final TextEditingController _emailController = final TextEditingController _emailController =
TextEditingController(); //email輸入text TextEditingController(); //email輸入text
@@ -35,15 +35,15 @@ class _PersonalInfoState extends State<PersonalInfo> {
print('傳遞過來的 email: ${widget.email}'); // 打印 email 來確認它是否正確傳遞 print('傳遞過來的 email: ${widget.email}'); // 打印 email 來確認它是否正確傳遞
final conn = await MySQLConnection.createConnection( final conn = await MySQLConnection.createConnection(
//host: '203.64.84.154', host: '203.64.84.154',
host: '10.0.2.2', //host: '10.0.2.2',
//127.0.0.1 10.0.2.2 //127.0.0.1 10.0.2.2
port: 3306, port: 33061,
userName: 'root', userName: 'root',
//password: 'Topic@2024', password: 'Topic@2024',
password: '0000', //password: '0000',
//databaseName: 'care', //testdb databaseName: 'care', //testdb
databaseName: 'testdb', //databaseName: 'testdb',
); );
await conn.connect(); await conn.connect();
@@ -51,18 +51,19 @@ class _PersonalInfoState extends State<PersonalInfo> {
try { try {
print('ok'); print('ok');
var result = await conn.execute( var result = await conn.execute(
'SELECT name, phone, age, gender, email FROM users WHERE email = :email', 'SELECT * FROM HomeLogin WHERE homeemail = :email',
{'email': widget.email}, // 傳入參數 email {'email': widget.email}, // 傳入參數 email
); );
if (result.rows.isNotEmpty) { if (result.rows.isNotEmpty) {
//有資料 //有資料
var row = result.rows.first; var row = result.rows.first;
setState(() { setState(() {
_name = row.colAt(0) ?? ''; //如果沒有資料就是空直 _name = row.colAt(0)??'';//如果沒有資料就是空直
_phone = row.colAt(1) ?? ''; _phone = row.colAt(1)??'';
_age = row.colAt(2) ?? ''; _gender = row.colAt(2)??'';
_gender = row.colAt(3) ?? ''; _address = row.colAt(3)??'';
_email = row.colAt(4) ?? ''; _email = row.colAt(4)??'';
_password = row.colAt(5)??'';
}); });
} }
} catch (e) { } catch (e) {
@@ -79,19 +80,19 @@ class _PersonalInfoState extends State<PersonalInfo> {
} }
void _saveChanges() async { void _saveChanges() async {
final conn = await MySQLConnection.createConnection( /*final conn = await MySQLConnection.createConnection(
host: '10.0.2.2', host: '203.64.84.154',
port: 3306, port: 33061,
userName: 'root', userName: 'root',
password: '0000', password: 'Topic@2024',
databaseName: 'testdb', databaseName: 'care',
); );
await conn.connect(); await conn.connect();*/
try { try {
await conn.execute( await conn.execute(
'UPDATE users SET email = :email WHERE email = :old_email', 'UPDATE HomeLogin SET homeemail = :email WHERE homeemail = :old_email',
{'email': _emailController.text, 'old_email': widget.email}, {'email': _emailController.text, 'old_email': widget.email},
); );
setState(() { setState(() {
@@ -106,6 +107,7 @@ class _PersonalInfoState extends State<PersonalInfo> {
} }
void _loginOut() async { void _loginOut() async {
print("loyout");
SharedPreferences prefs = await SharedPreferences.getInstance(); SharedPreferences prefs = await SharedPreferences.getInstance();
// remove all data in share preference(user data which save to identify user or others) // remove all data in share preference(user data which save to identify user or others)
prefs.clear(); prefs.clear();
@@ -164,13 +166,6 @@ class _PersonalInfoState extends State<PersonalInfo> {
onTap: () {}, onTap: () {},
), ),
Divider(), Divider(),
ListTile(
title: Text('生日'),
subtitle: Text(_age),
//trailing: Icon(Icons.arrow_forward_ios),
onTap: () {},
),
Divider(),
ListTile( ListTile(
title: Text('性別'), title: Text('性別'),
subtitle: Text(_gender), subtitle: Text(_gender),
@@ -179,26 +174,39 @@ class _PersonalInfoState extends State<PersonalInfo> {
), ),
Divider(), Divider(),
ListTile( ListTile(
title: Text('EMAIL'), title: Text('地址'),
subtitle: //Text(_email), subtitle: Text(_address),
_isEditing //trailing: Icon(Icons.arrow_forward_ios),
? TextField( onTap: () {},
controller: _emailController, ),
autofocus: true, Divider(),
onChanged: (value) { ListTile(
setState(() { title: Text('電子信箱'),
_email = value; subtitle: _isEditing
}); ? TextField(
}, controller: _emailController,
) autofocus: true,
: Text(_email), onChanged: (value) {
setState(() {
_email = value;
});
},
)
: Text(_email),
//trailing: Icon(Icons.arrow_forward_ios), //trailing: Icon(Icons.arrow_forward_ios),
onTap: () { onTap: () {
_toggleEdit(); // 切換到編輯狀態 _toggleEdit(); // 切換到編輯狀態
}, },
), ),
Divider(), Divider(),
ListTile(
title: Text('密碼'),
subtitle: Text(_password),
//trailing: Icon(Icons.arrow_forward_ios),
onTap: () {
},
),
Divider(),
SizedBox( SizedBox(
height: 20, height: 20,
width: 60, width: 60,