Differences between revisions 1 and 2
Revision 1 as of 2007-09-11 23:30:05
Size: 503
Editor: pool-70-104-110-44
Comment: stub
Revision 2 as of 2007-10-07 02:45:06
Size: 9054
Editor: pool-68-238-14-194
Comment: finally a start on content
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
#format rst
==================== ============== ============ ======== ========== ====
task postgresql sqlite3 mysql Oracle ODBC
==================== ============== ============ ======== ========== ====
install database
create database
command-line tool
GUI tool
install module
import
connect
get cursor
execute DML
use bind variables
commit
==================== ============== ============ ======== ========== ====

(content will follow, soon, really!)
================== ==================================================================================== ========================================================= =========================================================== ================================================================== ======================================================================
                                                                                             postgresql sqlite MySQL Oracle ODBC
================== ==================================================================================== ========================================================= =========================================================== ================================================================== ======================================================================
   create database createdb mydb created automatically when opened with sqlite3 created with Oracle XE install
------------------ ------------------------------------------------------------------------------------ --------------------------------------------------------- ----------------------------------------------------------- ------------------------------------------------------------------ ----------------------------------------------------------------------
 command-line tool psql -d mydb sqlite3 mydb.sqlite sqlplus scott/tiger@orcl
------------------ ------------------------------------------------------------------------------------ --------------------------------------------------------- ----------------------------------------------------------- ------------------------------------------------------------------ ----------------------------------------------------------------------
          GUI tool pgadmin3 mysql-admin
------------------ ------------------------------------------------------------------------------------ --------------------------------------------------------- ----------------------------------------------------------- ------------------------------------------------------------------ ----------------------------------------------------------------------
    install module easy_install psycopg2 included in Python 2.5 standard library easy_install mysql-python or apt-get install python-mysqldb easy_install cx_oracle
------------------ ------------------------------------------------------------------------------------ --------------------------------------------------------- ----------------------------------------------------------- ------------------------------------------------------------------ ----------------------------------------------------------------------
            import from psycopg2 import * from sqlite3 import * from MySQLdb import *
------------------ ------------------------------------------------------------------------------------ --------------------------------------------------------- ----------------------------------------------------------- ------------------------------------------------------------------ ----------------------------------------------------------------------
           connect conn = connect("dbname='testdb' user='me' host='localhost' password='mypassword'”) conn = connect('mydb.sqlite') or conn=connect(':memory:') conn=connect('scott/tiger@orcl') conn = odbc.odbc('DBALIAS') or odbc.odbc('DBALIAS/USERNAME/PASSWORD')
------------------ ------------------------------------------------------------------------------------ --------------------------------------------------------- ----------------------------------------------------------- ------------------------------------------------------------------ ----------------------------------------------------------------------
        get cursor curs = conn.cursor() curs = conn.cursor() curs = conn.cursor()
------------------ ------------------------------------------------------------------------------------ --------------------------------------------------------- ----------------------------------------------------------- ------------------------------------------------------------------ ----------------------------------------------------------------------
    execute SELECT curs.execute('SELECT * FROM tbl') curs.execute('SELECT * FROM tbl') curs.execute('SELECT * FROM tbl')
------------------ ------------------------------------------------------------------------------------ --------------------------------------------------------- ----------------------------------------------------------- ------------------------------------------------------------------ ----------------------------------------------------------------------
             fetch curs.fetchone(); curs.fetchall(); curs.fetchmany() curs.fetchone(); curs.fetchall(); curs.fetchmany() curs.fetchone(); curs.fetchall(); curs.fetchmany()
------------------ ------------------------------------------------------------------------------------ --------------------------------------------------------- ----------------------------------------------------------- ------------------------------------------------------------------ ----------------------------------------------------------------------
use bind variables curs.execute('SELECT * FROM tbl WHERE col = %(varnm)s', {'varnm':22}) curs.execute('SELECT * FROM tbl WHERE col = ?', [22]) curs.execute('SELECT * FROM tbl WHERE col = %s', [22]) curs.execute('SELECT * FROM tbl WHERE col = :varnm', {'varnm':22})
------------------ ------------------------------------------------------------------------------------ --------------------------------------------------------- ----------------------------------------------------------- ------------------------------------------------------------------ ----------------------------------------------------------------------
            commit conn.commit() (required) conn.commit() (required) conn.commit() (required)
------------------ ------------------------------------------------------------------------------------ --------------------------------------------------------- ----------------------------------------------------------- ------------------------------------------------------------------ ----------------------------------------------------------------------
----
CategoryDocumentation

================== ==================================================================================== ========================================================= =========================================================== ================================================================== ======================================================================

  • postgresql sqlite MySQL Oracle ODBC

================== ==================================================================================== ========================================================= =========================================================== ================================================================== ======================================================================

  • create database createdb mydb created automatically when opened with sqlite3 created with Oracle XE install







  • command-line tool psql -d mydb sqlite3 mydb.sqlite sqlplus scott/tiger@orcl







  • GUI tool pgadmin3 mysql-admin







  • install module easy_install psycopg2 included in Python 2.5 standard library easy_install mysql-python or apt-get install python-mysqldb easy_install cx_oracle







  • import from psycopg2 import * from sqlite3 import * from MySQLdb import *







  • connect conn = connect("dbname='testdb' user='me' host='localhost' password='mypassword'”) conn = connect('mydb.sqlite') or conn=connect(':memory:') conn=connect('scott/tiger@orcl') conn = odbc.odbc('DBALIAS') or odbc.odbc('DBALIAS/USERNAME/PASSWORD')







  • get cursor curs = conn.cursor() curs = conn.cursor() curs = conn.cursor()







  • execute SELECT curs.execute('SELECT * FROM tbl') curs.execute('SELECT * FROM tbl') curs.execute('SELECT * FROM tbl')







  • fetch curs.fetchone(); curs.fetchall(); curs.fetchmany() curs.fetchone(); curs.fetchall(); curs.fetchmany() curs.fetchone(); curs.fetchall(); curs.fetchmany()







use bind variables curs.execute('SELECT * FROM tbl WHERE col = %(varnm)s', {'varnm':22}) curs.execute('SELECT * FROM tbl WHERE col = ?', [22]) curs.execute('SELECT * FROM tbl WHERE col = %s', [22]) curs.execute('SELECT * FROM tbl WHERE col = :varnm', {'varnm':22})







  • commit conn.commit() (required) conn.commit() (required) conn.commit() (required)








CategoryDocumentation

DbApiCheatSheet (last edited 2010-03-09 03:54:30 by bb14f857)

Unable to edit the page? See the FrontPage for instructions.