Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python sqlite - quotes usage in execute() method

While reading through python sqlite3 DB-API, the first example of creating table uses thee single quotes:

c.execute('''CREATE TABLE stocks
             (date text, trans text, symbol text, qty real, price real)''')

But in other examples there are double quotes:

cur.execute("create table people (name_last, age)")

So I've got 2 questions:

  1. It this really any difference? How can it affect create table command?
  2. Which one is better to use with parameters? ex.:

    c.execute('''CREATE TABLE %s(date text, trans text, symbol text, qty real, price real)''' % table_name)

VS.

 cur.execute("create table %s (name_last, age)" % table_name)

Thanks

like image 539
Samuel Avatar asked Dec 07 '25 10:12

Samuel


2 Answers

There is basically no rule for double or single quotes.

But three consecutive quotes start multiline quoted values.

like image 135
rfkortekaas Avatar answered Dec 09 '25 23:12

rfkortekaas


Is this really any difference? How can it affect create table command?

Using single/double quoted string or a triple-quoted string doesn't affect the create command in any way. They are ultimately just string arguments to the create command.

Which one is better to use with parameters?

Double quotes are the norm in most of the python programs. Triple quotes are used mostly when you have a big multi-line string copy-pasted from somewhere else and you want to embed them in your program. In the link that you gave above, we can see one such example in the usage in cursor.executescript(), where an sql script (which probably existed before) is used within the program.

like image 27
Ramakrishnan G Avatar answered Dec 10 '25 00:12

Ramakrishnan G