0

I think I am stucked on an easy job as a beginner but have to ask this question. My objective is to create another list from data obtained from a SQL Table. This list will be created in acocrdance with the input data by user.

The SQL table has no problem, but I couldn't write on a txt file despite not receiving an error message. Where is the problem?

import sqlite3
db = sqlite3.connect("air2.sql")
cs = db.cursor()


epcs = dict()
for i in range(19):
    epcs[i] = 0

def addepc():

    epcNo = input("EPC No:")
    a = "SELECT * FROM 'epcval5' WHERE epc='EPC{}'".format(epcNo)

    cs.execute(a)
    data = cs.fetchone()
    print("You have selected this EPC:")
    for i in data:
        print(i)

    b = "SELECT value FROM 'epcval5' as float WHERE epc='EPC{}'".format(epcNo)
    cs.execute(b)
    epcv = cs.fetchone()
    res = str('.'.join(str(ele) for ele in epcv))
    print(type(res))

    with open('epcs.txt', 'w') as f:
        epcs[epcNo] = res
        f.write(epcs[epcNo])

addepc()
        print("Done! Press ENTER to continue")

        input()
5
  • "epcs" isn't defined in the shown code. Commented Apr 19, 2020 at 13:19
  • Oh sorry about that, now it should be OK. Commented Apr 19, 2020 at 13:54
  • And the error is? Commented Apr 19, 2020 at 13:57
  • No error, just can't write to the text file Commented Apr 19, 2020 at 13:58
  • Yeah interstingly now it works, thank you for your valuable comments anyway Commented Apr 19, 2020 at 14:36

1 Answer 1

1

You could use pandas to write it to a csv file

with sqlite3.connect(DB_FILENAME) as con:
    df = pd.read_sql_query('your sql query',con)

df.to_csv('file_name')
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for recommendation, but the text file looks updated now. I still don't understand why it looks like unwritten at the begining. Sorry for trouble fellas...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.