Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #79360
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Subject | Re: Is there an easy way to control indents in Python |
| Date | 2014-10-20 21:13 -0400 |
| Organization | IISS Elusive Unicorn |
| References | (3 earlier) <54424691$0$12978$c3e8da3$5496439d@news.astraweb.com> <bd24df51-bdee-4ccc-82f7-28bf6dbd8bf5@googlegroups.com> <CALwzid=oS6H-ETtTPsRPawas-Gkj=7hkTWcwLmE8vZ7NdR5R+g@mail.gmail.com> <CALwzidmvwEfnsHExNdzDY8bs5YZ12QX_-rx3Ur+Hpz6Cg+WGdQ@mail.gmail.com> <CAAp0bGvHZd66Dw4Z5wriDiMS4Xm50rKjNu+K7_L9scyMdXjWXw@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.15020.1413853991.18130.python-list@python.org> (permalink) |
On Mon, 20 Oct 2014 17:04:09 -0200, Juan Christian
<juan0christian@gmail.com> declaimed the following:
>Ok, new code using ?:
>
>import sqlite3
>
>db = sqlite3.connect('db.sqlite')
>
>
>def create_db():
Misnamed function... the "database" was created (if it didn't exist)
when you did the .connect() call. Here you are defining the schema of a
table.
> db.execute('''
> CREATE TABLE TOPICS(
> ID INT PRIMARY KEY NOT NULL,
> URL VARCHAR NOT NULL,
> AUTHOR VARCHAR NOT NULL,
> MESSAGE VARCHAR NOT NULL
> );
> ''')
>
Stylistic point -- I never use the connection.execute() form; as
documented:
execute(sql[, parameters])
This is a nonstandard shortcut that creates an intermediate cursor
object by calling the cursor method, then calls the cursor’s execute method
with the parameters given.
More portable to create an actual cursor
cur = db.cursor()
and then
cur.execute(...)
>
>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))
Presuming the adapter is expecting a tuple of parameters, you haven't
provided one. Try with
..., (_id,) )
It is the comma, not the () that make a tuple.
> cursor = db.execute("SELECT ID, URL, AUTHOR, MESSAGE FROM TOPICS WHERE
>ID = ?", (_id))
>ValueError: parameters are of unsupported type
>
{see prior comment}
>-------------
>
>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
Well, sure... If it failed on the "select" statement you had already
executed (and commited) the table create and the insert of one row.
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Is there an easy way to control indents in Python ryguy7272 <ryanshuell@gmail.com> - 2014-10-14 19:13 -0700
Re: Is there an easy way to control indents in Python Juan Christian <juan0christian@gmail.com> - 2014-10-14 23:23 -0300
Re: Is there an easy way to control indents in Python alex23 <wuwei23@gmail.com> - 2014-10-15 17:27 +1000
Re: Is there an easy way to control indents in Python Chris “Kwpolska” Warrick <kwpolska@gmail.com> - 2014-10-15 16:32 +0200
Re: Is there an easy way to control indents in Python alex23 <wuwei23@gmail.com> - 2014-10-16 09:51 +1000
Re: Is there an easy way to control indents in Python Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-10-17 18:22 +1300
Re: Is there an easy way to control indents in Python Terry Reedy <tjreedy@udel.edu> - 2014-10-15 13:12 -0400
Re: Is there an easy way to control indents in Python Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-15 13:30 -0600
Re: Is there an easy way to control indents in Python Simon Kennedy <sffjunkie@gmail.com> - 2014-10-16 07:08 -0700
Re: Is there an easy way to control indents in Python Chris Angelico <rosuav@gmail.com> - 2014-10-17 02:02 +1100
Re: Is there an easy way to control indents in Python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-18 21:53 +1100
Re: Is there an easy way to control indents in Python Simon Kennedy <sffjunkie@gmail.com> - 2014-10-20 08:54 -0700
Re: Is there an easy way to control indents in Python Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-20 11:54 -0600
Re: Is there an easy way to control indents in Python Simon Kennedy <sffjunkie@gmail.com> - 2014-10-21 01:45 -0700
Re: Is there an easy way to control indents in Python Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-21 11:09 -0600
Re: Is there an easy way to control indents in Python Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-20 11:57 -0600
Re: Is there an easy way to control indents in Python Juan Christian <juan0christian@gmail.com> - 2014-10-20 17:04 -0200
Re: Is there an easy way to control indents in Python Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-20 13:40 -0600
Re: Is there an easy way to control indents in Python MRAB <python@mrabarnett.plus.com> - 2014-10-20 20:43 +0100
Re: Is there an easy way to control indents in Python Juan Christian <juan0christian@gmail.com> - 2014-10-20 20:30 -0200
Re: Is there an easy way to control indents in Python Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-10-20 21:13 -0400
Re: Is there an easy way to control indents in Python Chris Angelico <rosuav@gmail.com> - 2014-10-15 13:18 +1100
Re: Is there an easy way to control indents in Python Dan Stromberg <drsalists@gmail.com> - 2014-10-14 19:38 -0700
Re: Is there an easy way to control indents in Python Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-10-14 21:35 -0500
Re: Is there an easy way to control indents in Python Chris Angelico <rosuav@gmail.com> - 2014-10-15 13:47 +1100
Re: Is there an easy way to control indents in Python jumppanen.jussi@gmail.com - 2014-10-21 06:51 -0700
csiph-web