3

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

3 Answers 3

3

There is basically no rule for double or single quotes.

But three consecutive quotes start multiline quoted values.

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

Comments

1
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.

Comments

1

using three """ is like commenting out a line

#this text python doesn't read so you can write about your code

"""it allows
you to type
over multiple
lines."""

Also you can use " or ' python see them both the same as long as you close with the same style.

2 Comments

Three quotes can also be used for multiline comments. But you cannot say three quotes is always a comment!
@Warmee Just for the record, these are docstrings. They aren't exactly comments, they are used when the documentation is created automatically. I'd refrain from even suggesting their usage as comments.

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.