简体   繁体   中英

How to fetch data from SQL Database? (Troubleshoot)

I have created tables using SQLite Queries as shown below:

import sqlite3 as lite
import sys

con = lite.connect('Records.db')

with con:
    cur = con.cursor()    
    cur.execute("CREATE TABLE Users(User_Id INTEGER PRIMARY KEY, Username STRING, Password STRING, Acc_Type STRING, First_Name STRING, Surname STRING, Class STRING, FullName STRING)")
    cur.execute("INSERT INTO Users VALUES(1, 'Admin', 'PassWord567', 'Admin', '', 'Admin', 'None', 'Admin')")
    cur.execute("INSERT INTO Users VALUES(2, 'HamzahA12', 'password', 'Student', 'Hamzah', 'Akhtar', '13E2', 'Hamzah Akhtar')")
    cur.execute("CREATE TABLE Questions(Question_Id INTEGER PRIMARY KEY, Question STRING, Answer STRING, Mark INTEGER, Topic STRING, Incorrect STRING, QType STRING)")
    cur.execute("INSERT INTO Questions VALUES(1, 'What is 2/3 of 6?', '4', '1', 'Fractions', 'None', 'Numerical')")
    cur.execute("INSERT INTO Questions VALUES(2, 'What is 5/4 of 20?', '25', '1', 'Fractions', 'None', 'Numerical')")
    cur.execute("CREATE TABLE Tests(Test_Id INTEGER PRIMARY KEY, TestName STRING, TotalMarks INTEGER, Calculator STRING)")
    cur.execute("INSERT INTO Tests(TestName) VALUES('Test 1')")
    cur.execute("INSERT INTO Tests(TestName) VALUES('Test 2')")

However, when it comes to fetching data, for some reason, i can fetch data from the User table or Question table, but cannot fetch data from the Test table. I use the following code to fetch data from the tables:

con = lite.connect('Records.db')
            with con:
                cur = con.cursor()
                cur.execute("SELECT TestName FROM Tests")
                rows = cur.fetchall()
                for row in rows:
                    Testname = str(row)
                    Testname = Testname[2:-3]
                    print(Testname)

In my code to fetch the data, i use [2:-3] to remove the apostrophes and brackets from the fetched data. For some reason, when fetched, it returns [] . Any ideas as to what im doing wrong?? By the way, when using the same method (including the [2:-3] ) to fetch data from other tables like Question table or User table, there is no issue...???

Converting the row list into a string, and then removing the punctuation generated by that conversion, is an extremely contrived way of extracting values.

Just extract the value directly from the row:

for row in rows:
    testname = row[0]
    ...

I used the following to solve my problem: (Inserted Test_Id with TestName )

cur.execute("CREATE TABLE Tests(Test_Id INTEGER PRIMARY KEY, TestName STRING, TotalMarks INTEGER, Calculator STRING)")
cur.execute("INSERT INTO Tests(Test_Id, TestName) VALUES(1, 'Test 1')")
cur.execute("INSERT INTO Tests(Test_Id, TestName) VALUES(1, 'Test 2')")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM