0

I created a sqlite3 table using variable name, I have also inserted some values, I want to read the tables row values using the table's variable name.

created a table as shown below.....successful the table name is passed through kivy gui.

def exams_table(name):    
    x_table = '''CREATE TABLE {}(
        user_id INTEGER PRIMARY KEY,
        account VARCHAR(20) NOT NULL,
        l_name VARCHAR(20) NOT NULL,
        f_name VARCHAR(20) NOT NULL,
        class VARCHAR(20) NOT NULL,
        term VARCHAR(20) NOT NULL,
        exam VARCHAR(20) NOT NULL,
        math REAL(20) NOT NULL,
        eng REAL(20) NOT NULL,
        comp REAL(20) NOT NULL,
        eng_t REAL(20) NOT NULL,
        kis REAL(20) NOT NULL,
        ins REAL(20) NOT NULL,
        kis_t REAL(20) NOT NULL,
        sst REAL(20) NOT NULL,
        r_edu REAL(20) NOT NULL,
        s_r_total REAL(20) NOT NULL,
        science REAL(20) NOT NULL,
        creative REAL(20) NOT NULL,
        e_read REAL(20) NOT NULL,
        s_read REAL(20) NOT NULL,
        total_m REAL(20) NOT NULL,
        mean_s REAL(20) NOT NULL,
        pos INTEGER(20) NOT NULL,
        total_p INTEGER(20) NOT NULL);'''.format(name)
    cursor.execute(x_table)

inserted values as shown below... successfully

cursor.execute('INSERT INTO %s(user_id, account, l_name, f_name, class, '
                        'term, exam, math, eng, comp, eng_t, kis, ins, kis_t, sst, r_edu, s_r_total, science, creative,'
                        'e_read, s_read, total_m, mean_s, pos, total_p)VALUES(NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,'
                        '?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' % xam_table_name,
                        (self.pupil_number.text, c[0], c[1], c[2], self.exam_term.text, self.exam_type.text, self.math.text,
                        self.eng.text, self.eng_com.text, self.english_total(), self.kis.text, self.kis_com.text, self.swahili_total(), self.sst.text,
                        self.re.text, self.sst_re_total(), self.sci.text, self.crt.text, self.reading.text, self.s_reading.text,
                        self.total.text, self.mean.text, '', ''))
                        conn.commit()

now I want to sues these values, I am reading the as shown below

cursor.execute('select * from %s' % xam_table_name)
        f = cursor.fetchall()
        print(f)

and I get the error below

cursor.execute('select * from %s' % xam_table_name)
 sqlite3.OperationalError: near " ": syntax error

I need to use the values to generate a pdf report..... kindly help, thanks

1
  • How can i read the values using tables variable name, the table name is entered by user through a kivy gui Commented Aug 6, 2019 at 11:02

1 Answer 1

1

It's hard to read your code, but my guess is that by the time you do the select, your variable xam_table_name doesn't contain what you think it contains. Put a print(xam_table_name) line above your cursor.execute(...) line to double-check that xam_table_name contains what you think it does at that point.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks a lot, the variable name had a value same as the name of the table created, but I don't know what could be the difference between the two values... I tried converting the variable value to str() and it works.... may be one can explain it, in what data type was the variable value, before I converted it to string?
Does your development environment support debug breakpoints? It would be interesting to pause execution at the line above and test the content of the variable to see what is actually stored in it.

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.