maurelio79
maurelio79

Reputation: 292

sqlite3 simple query in python hangs

i'm trying to make a simple query in python and sqlite3:

#!/usr/bin/env python
# -*- coding: utf-8; -*-

import sqlite3

db = sqlite3.connect('test.db')
query = """CREATE TABLE `home` (
   `id` int(11) not null auto_increment,
   `full_name` char(255) not null,
   `display_name` char(255),
   `ip_address` char(255) not null,
   `user`  char(255) not null,
   PRIMARY KEY (`id`)
);"""
db.execute(query)
db.commit()
db.close()

But when i run the script, nothing happens; i mean: a file called test.db is created in the directory, but after that the shell remain there without return anything (even the prompt) and i need to kill the script with kill -9

Any help?

Upvotes: 1

Views: 2307

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121486

I don't know why your script apparently hangs, but there are SQL syntax errors in your query:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
sqlite3.OperationalError: near "auto_increment": syntax error

SQLite only have a AUTOINCREMENT keyword, and that only is supported after the PRIMARY KEY constraint; you'll have to move your PRIMARY KEY line from the bottom to the id column. You'll have to use the proper INTEGER type as well, instead of int.

SQLite also ignores column sizes, you may as well drop those.

The following query works:

query = """CREATE TABLE `home` (
   `id` integer primary key autoincrement,
   `full_name` char(255) not null,
   `display_name` char(255),
   `ip_address` char(255) not null,
   `user`  char(255) not null
);"""

where I left in the column sizes for the char columns, but you may as well make those TEXT (without a size) and be done with it.

Upvotes: 2

Related Questions