Français

MySQL adapter

The main difference with the pure-Python module is the syntax to identify a database and a table, and the need to specify field types on base creation

For compliance with MySQL vocabulary, the module defines three classes, Connection, Database and Table

Connection(host,login,password) : returns a connection to the MySQL server

Instances of Connection support 2 methods :

  • databases() : returns the list of the databases names
  • create(db_name) : creates a database named db_name and returns the instance of the Database class

Database(db_name, connection) : returns the database db_name for the specified connection

Instances of Database support 2 methods:

  • tables() : returns the list of the table names in the database
  • drop() : drop the database

Table(table_name,db) : table_name is the table name, db is the instance of Database

Manipulating a table uses the same syntax as in PyDbLite :

  • to create the table you must specify a MySQL field type : INTEGER, REAL, DATETIME, CHAR, BLOB,etc. :
    table.create(('name','CHAR(20)'),('age','INTEGER'),('size','REAL'))
  • if other information needs to be provided, put it in the second argument, using the SQL syntax for MySQL :
    table.create(('recid','INTEGER PRIMARY KEY AUTO_INCREMENT'),
         ('date','TIMESTAMP DEFAULT CURRENT_TIMESTAMP'))

For record insertion, selection, update and deletion, adding or dropping fields, the syntax is the same as with the pure-Python module

Selection by record id only works if a field INTEGER PRIMARY KEY AUTO_INCREMENT is present in the table ; the functions delete() and update() also require this record id

The Table instance has an attribute cursor, so you can also execute SQL expressions by table.cursor.execute(some_sql) and get the result by results = table.cursor.fetchall()