zoukankan      html  css  js  c++  java
  • Python连接MySQL的实例代码

    MySQLdb下载地址:http://sourceforge.net/projects/mysql-python/
    下载解压缩后放到%Python_HOME%/Lib/site-packages目录中,python会自动找到此包。
    MySQLdb基本上是MySQL C API的Python版,遵循Python Database API Specification v2.0。

    其他:

    1. 平台及版本
    linux 内核2.6,gcc 3.4.4,glibc 2.4
    python 2.4.3
    mysql 5.0.19
    mysql-python 1.2.1-p2


    2. 安装mysql-python
    tar xvfz MySQL-python-1.2.1_p2.tar.gz
    cd MySQL-python-1.2.1_p2
    python setup.py build
    python setup.py install

    3. 使用
    import MySQLdb


    3.1. 连接
    conn = MySQLdb.Connection(host, user, password, dbname)


    3.2. 选择数据库
    conn.select_db(’database name’)


    3.3. 获得cursor
    cur = conn.cursor()


    3.4. cursor位置设定
    cur.scroll(int, mode)
    mode可为相对位置或者绝对位置,分别为relative和absolute。


    3.5. select
    cur.execute(‘select clause’)
    例如
    cur.execute(‘select * from mytable’)


    row = cur.fetchall()
    或者:
    row1 = cur.fetchone()


    3.6. insert
    cur.execute(‘inset clause’)
    例如
    cur.execute(‘insert into table (row1, row2) values (/’111/’, /’222/’)’) 

    conn.commit() 

    3.7. update
    cur.execute(‘update clause’)
    例如
    cur.execute(“update table set row1 = ‘’ where row2 = ‘row2 ‘ ”) 

    conn.commit()


    3.8. delete
    cur.execute(‘delete clause’)
    例如
    cur.execute(“delete from table where row1 = ‘row1’ ”)


    conn.commit() 

    http://www.python123.cn/PythonInternet/20090609_11.html 

    作者:wenhai_zhang 发表于2009-12-14 21:25:00 原文链接
    阅读:601 评论:0 查看评论
  • 相关阅读:
    在 Flink 算子中使用多线程如何保证不丢数据?
    日处理数据量超10亿:友信金服基于Flink构建实时用户画像系统的实践
    Java编码技巧之高效代码50例
    codeforces 1284D. New Year and Conference(线段树)
    codeforces 1284C. New Year and Permutation(组合数学)
    codeforces 1284B. New Year and Ascent Sequence(二分)
    Codeforces Hello2020 A-E简要题解
    POJ2456 Aggressive cows(二分)
    POJ3122 Pie(二分)
    POJ3258 River Hopscotch(二分最大化最小值)
  • 原文地址:https://www.cnblogs.com/wenhaizhang/p/2099158.html
Copyright © 2011-2022 走看看