Kunal
Kunal

Reputation: 607

Some bug in retriving values from SQLite database in python

I am trying to insert tuple in database. It doesn't give any error in the code. But the output contains some dummy character while printing the whole row. Output is also copied in the post. Please help me in figuring out the bug in the code. This is a dummy code for a big project.

Code:

import sqlite3 as sql

def foo():
    db = sql.connect('test.db')
    db.execute('drop table if exists test')
    db.execute('create table test (t1 text, i1 int)')

    str = """insert into test(t1,i1) values ('one',1 ) """
    db.execute(str)
    db.execute('insert into test(t1,i1) values (?, ?)', ('two',2))

    db.commit()

    cursor = db.execute('select * from test')

    for row in cursor:
        print row

Output:

(u'one', 1)
(u'two', 2)

As shown in the output, The expected output of the code is the a tuple of two elements. Instead there is some character 'u' in the output.

Thanks

Upvotes: 2

Views: 92

Answers (1)

Andrew Clark
Andrew Clark

Reputation: 208695

The u prefix on a string means that it is a Unicode string, you still have a two-element tuple as expected.

By default the sqlite3 module returns text as Unicode strings. If you want to receive byte strings encoded in utf-8 instead, you can set the connection's text_factory attribute to str.

Upvotes: 2

Related Questions