MySQLdb warnings ... what caused them?

Gerhard Häring gh at ghaering.de
Tue May 13 17:06:56 EDT 2003


Sheila King wrote:

> OK, let's just say I "solved" this problem, and would like to crawl into a
> hole now and hide.
> 
> However, in fairness to the newsgroup, I will show the problem that caused
> the warning (it was a data truncation problem):
> 
> mysql> DESCRIBE test
>     -> ;
> +-------+-------------+------+-----+---------+-------+
> | Field | Type        | Null | Key | Default | Extra |
> +-------+-------------+------+-----+---------+-------+
> | ID    | varchar(4)  | YES  |     | NULL    |       |
> | name  | varchar(8)  | YES  |     | NULL    |       |
> | phone | varchar(10) | YES  |     | NULL    |       |
> +-------+-------------+------+-----+---------+-------+
 > [...]

Better use something like "id auto_increment primary key" for the ID 
column. You can then insert data with just:

cu = cx.cursor()
cu.execute("""
	INSERT INTO TEST(ID, NAME, PHONE)
		VALUES (NULL, %s, %s)
	""", ("Alice", "4711"))

The NULL for the ID column will be replaced by MySQL with the default 
value for this column. Which is the next-greatest value of the hidden 
sequence for the ID column that "auto_increment" creates. [1]

You probably also want to create (unique) indices on your primary key 
columns, but you'll all get to know that while learning SQL.

If you'll find that MySQL is too limited for your needs, I'd suggest you 
take a look at more powerful and feature-complete Open Source RDBMS, 
like PostgreSQL (or Firebird, or SAPdb).

-- Gerhard

[1] Dunno if this sucker really has sequences or how it works 
internally, but SEQUENCE is the term you'd use in a real database :-)





More information about the Python-list mailing list