Home > other >  Python is unable to read properly the mysql database data?? Pray god to help answer
Python is unable to read properly the mysql database data?? Pray god to help answer

Time:09-20

New white posts for the first time, have to write a bad place please forgive me
Things like this:
1. I want to realize functions:
Students enter student id to access the database, if can match in the database to the student id, return "welcome to system"; Otherwise, return to the "I",
2. What I do:
(1) I was in my SQL build the student table, defines the s_no, s_name attributes such as
(2) I use pymysql link python code and database, the connection is successful
(3) in pycharm, use the input input s_no "1001" (this is defined in the mysql student id right)
3. Problems:
I input the database defined in s_no (that is, the right s_no), system returns is "I"? Excuse me what reason be?

The following is the code:
The import pymysql
DBHOST="localhost"
DBUSER="root"
DBPASS="123456"
DBNAME="book_manage_system"
The db=pymysql. Connect (DBHOST, DBUSER, DBPASS, DBNAME)
Cursor=the cursor ()

The class Identity (object) :

# students authentication methods are defined with the class method
@ classmethod
Def StudentIdentity (CLS) :
Username2=input (" please enter the student number: ")

# call database "s_no
"SQL="select s_no from student"
Cursor. The execute (SQL)
Result=cursor. Fetchall ()
Print (result)

# to judge the user to enter the student id is correct?
For I in result:
If I==username2:
Print (" hello, welcome to the books management system management system ")

Print (" your student id is not correct, enter again ")
CLS. StudentIdentity ()

# instantiation objects
Xiaoming=Identity ()
Xiaoming. StudentIdentity ()
Print (" xiaoming ")

It's run results: please enter the student id: 1001
Your student id is not correct to enter

CodePudding user response:

Don't talk about logic, according to your current writing, change

 

# to judge the user to enter the student id is correct?
For I in result:
If I==username2:
Print (" hello, welcome to the books management system management system ")



Change

 

# to judge the user to enter the student id is correct?
For rec in the result:
If rec [0]==username2:
Print (" hello, welcome to the books management system management system ")




If the database, there are 1001 student id
Should be able to see the hello, welcome to the tip of books management system management system,



Code according to your logic, after watching the above hints, then have your student id is incorrect, re-enter your tips,

CodePudding user response:

Appreciate great god above reply, but after enter 1001 or display input incorrect

CodePudding user response:

The following code, I ran past, yes, you try (https://blog.csdn.net/weixin_42261305/article/details/105488253)

The import pandas as pd
The import pymssql

Sqlconnect=pymssql. Connect (DBHOST, DBUSER, DBPASS, DBNAME) # server name, account, password and database name
If sqlconnect:
Print (" connection success!" )


The class Identity (object) :
# students authentication methods are defined with the class method
@ classmethod
Def StudentIdentity (CLS) :
Username2=input (" please enter the student number: ")
# call database "s_no
"SQL="'
The select s_no from student
"'
Result=pd. Read_sql (SQL, sqlconnect)
# to judge the user to enter the student id is correct?
If username2 in result. Values:
Print (" hello, welcome to the books management system management system ")
The else:
Print (" your student id is not correct, enter again ")

# instantiation objects
Xiaoming=Identity ()
Xiaoming. StudentIdentity ()
Print (" xiaoming ")
Sqlconnect. Close ()

CodePudding user response:

Or query failure
  • Related