vendredi 2 octobre 2015

Reading raw data from an sqlite table in python

I am currently trying to make a logon system, just to learn some more advanced python script.

Originally, I was using .txt files to save all of the information, but that ended up being messy and unsecured.

After I completed the first set of script, I decided to try saving the information to a database file. I have figured out how to read data from the table but it comes out with [(''),] around it.

This is an issue because, I need to use the raw data to compare to an input, which cannot be done with the symbols around it.

So, I was wondering if it is possible to read the data as a raw string, without the symbols. I have been doing this in a separate file to the main project. Here is the contents of said file:

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)

And here is the output:

What is your username: James
Select the message you wish to read: LOL

Sender: [('Kieran',)]
Recipient: [('James',)]
Subject: [('LOL',)]

Message: [('Hey',)]

This is all the correct data, but I need to remove the [(''),] to be able to compare it. I am using Python 3.4.2.

Aucun commentaire:

Enregistrer un commentaire