Is there an easy way to control indents in Python

MRAB python at mrabarnett.plus.com
Mon Oct 20 21:43:36 CEST 2014


On 2014-10-20 20:04, Juan Christian wrote:
> Ok, new code using ?:
>
> import sqlite3
>
> db = sqlite3.connect('db.sqlite')
>
>
> def create_db():
>      db.execute('''
> CREATE TABLE TOPICS(
> ID INT PRIMARY KEY NOT NULL,
> URL VARCHAR NOT NULL,
> AUTHOR VARCHAR NOT NULL,
> MESSAGE VARCHAR NOT NULL
> );
> ''')
>
>
> def insert_db(_id, url, author, message):
>      db.execute("INSERT INTO TOPICS (ID, URL, AUTHOR, MESSAGE) VALUES
> (?, ?, ?, ?)", (_id, url, author, message))
>      db.commit()
>
>
> def get_db(_id):
> cursor = db.execute("SELECT ID, URL, AUTHOR, MESSAGE FROM TOPICS WHERE
> ID = ?", (_id))
> return cursor.fetchone()
>
>
> if __name__ == '__main__':
> create_db()
> insert_db(12, 'abc.com <http://abc.com>', 'a', 'b')
> print(get_db(12))
> db.close()
>
> -------------
>
> But now when I execute I get
>
> Traceback (most recent call last):
>    File ".\sql.py", line 30, in <module>
>      print(get_db(12))
>    File ".\sql.py", line 23, in get_db
>      cursor = db.execute("SELECT ID, URL, AUTHOR, MESSAGE FROM TOPICS
> WHERE ID = ?", (_id))
> ValueError: parameters are of unsupported type
>
> -------------
>
I'm not certain, but I think that the SQL type is called "INTEGER", not
"INT".

> And the second time, again, I get
>
> Traceback (most recent call last):
>    File ".\sql.py", line 28, in <module>
>      create_db()
>    File ".\sql.py", line 14, in create_db
>      ''')
> sqlite3.OperationalError: table TOPICS already exists
>
That's because you created the table the last time you ran it.




More information about the Python-list mailing list