you're right, is the reason why coding. But when database connect, charset='GB2312', my other sql statements will display, only this one is null, charset is set to utf8, the data from the database will be garbled
def enshidata(self):
sqlenshi ="select top 10 r.STCD,sum(r.DRP) as DRP_SUM ,b.STNM,b.STLC,b.ADMAUTH from RWDB.dbo.ST_PPTN_R r left join RWDB.dbo.ST_STBPRP_B b on r.STCD=b.STCD where r.TM >= DATEADD(hour,-1,GETDATE()) and b.ADMAUTH ='建始县' group by r.STCD,b.STNM,b.STLC,b.ADMAUTH order by sum(r.DRP) desc"
# 执行语句
self.cursor.execute(sqlenshi)
# 获取所有记录
resultses = self.cursor.fetchall()
print(resultses)
return resultses
python Flask framework calls sql statements, checks all the code, and everything is fine. print, however, will have a null value, as shown below:
The
sql statement has been tested in the database, is fine, and has a return value. Other sql statements are returned, such as the simple select * from sql statement is returned. What's the problem?
0 Answer
you're right, is the reason why coding. But when database connect, charset='GB2312', my other sql statements will display, only this one is null, charset is set to utf8, the data from the database will be garbled
suggest all use utf8
Yes, I think I've found the problem now. It's a coding problem. When charset='GB2312' in connect, other sql statements will be displayed. Only the sql statement with Chinese characters will have a null value. But when I set the charset to utf-8, getting the database data was garbled
Look it up to see what the encoding was when the database was created. Then, when the code is connected, try the same encoding again
Obviously, the Chinese in the sql submitted by your program becomes the code \xbd\xa8\xca\xbc\xcf\xd8, This value will not be found in the database, and the return will always be null. The next thing to solve is to solve the problem of sql garble submitted
I looked at the answers and found that
1, the database configuration and links are OK, the database is correct, the table names are correct,
2, the sql statements are OK, The sql copy to navicat executes normally
is that what I understand?
print out your sql print in the program, and then take the printed sql, go to the database to see if the result return
"select top 10 r.STCD,sum(r.DRP) as DRP_SUM ,b.STNM,b.STLC,b.ADMAUTH from RWDB.dbo.ST_PPTN_R r left join RWDB.dbo.ST_STBPRP_B b on r.STCD=b.STCD where r.TM > = DATEADD(hour, 1, GETDATE()) and b.A DMAUTH = 'jianshi county' group by r.S TCD, b.S TNM, b.S TLC, b.A DMAUTH order by the sum(r.D RP) desc
"b"select top 10 r.STCD,sum(r.DRP) as DRP_SUM ,b.STNM,b.STLC,b.ADMAUTH from RWDB.dbo.ST_PPTN_R r left join RWDB.dbo.ST_STBPRP_B b on r.STCD=b.STCD where r.TM > = DATEADD(hour,-1,GETDATE()) and b.ADMAUTH ='\xbd\xa8\xca\xbc\xcf\xd8' group by r.STCD,b.STNM,b.STLC,b.ADMAUTH order by sum(r.DRP) desc"
这家伙很懒,什么都没留下...