我目前正在尝试制作一个登录系统,只是为了学习一些更高级的Python脚本。
最初,我使用.txt
文件保存所有信息,但这样做很混乱且不安全。
在完成第一组脚本后,我决定尝试将信息保存到数据库文件中。我已经想出了如何从表中读取数据,但它会带着[(''),]
符号。
这是个问题,因为我需要用原始数据来比较输入,而这不能与符号一起完成。
因此,我想知道是否可能将数据作为原始字符串读取,而不带符号。我已经在主项目的一个单独文件中进行了操作。以下是该文件的内容:
import sqlite3 as sql
con = sql.connect(r"E:\Users.db")
c = con.cursor()
con.row_factory = sql.Row
username = input("What is your username: ")
readdata = input("Select the message you wish to read: ")
c.execute("SELECT * FROM Mail WHERE (Subject LIKE '"+readdata+"') AND (Recipient LIKE '"+username+"')")
component = c.fetchall()
for row in component:
c.execute("SELECT Recipient FROM Mail WHERE (Subject LIKE '"+readdata+"') AND (Recipient LIKE '"+username+"')")
rc = list(c.fetchall())
rec = str(rc)
c.execute("SELECT Sender FROM Mail WHERE (Subject LIKE '"+readdata+"') AND (Recipient LIKE '"+username+"')")
snt = list(c.fetchall())
sent = str(snt)
c.execute("SELECT Subject FROM Mail WHERE (Subject LIKE '"+readdata+"') AND (Recipient LIKE '"+username+"')")
sbject = list(c.fetchall())
subject = str(sbject)
c.execute("SELECT Message FROM Mail WHERE (Subject LIKE '"+readdata+"') AND (Recipient LIKE '"+username+"')")
mg = list(c.fetchall())
msg = str(mg)
print()
print("Sender: " + sent)
print("Recipient: " + rec)
print("Subject: " + subject)
print()
print("Message: " + msg)
以下是输出结果:
What is your username: James
Select the message you wish to read: LOL
Sender: [('Kieran',)]
Recipient: [('James',)]
Subject: [('LOL',)]
Message: [('Hey',)]
这是全部正确的数据,但我需要删除[(''),]
才能进行比较。我正在使用Python 3.4.2。
fetchone()
是什么意思?:) 我之前总是做类似于a = c.fetchall(); a.items()[0]
这样的操作。 - Remi Guancursor.fetchone()
文档。请注意,如果没有更多的结果可以获取,它将返回None
。 - Martijn Pieters