• python MySQLdb


    # -*- coding: utf-8 -*-
    #mysqldb
    import time, MySQLdb

    #连接
    conn=MySQLdb.connect(host="localhost",user="root",passwd="root",db="test",charset="utf8")
    cursor = conn.cursor()

    #删除表
    sql = "drop table if exists user"
    cursor.execute(sql)

    #创建
    sql = "create table if not exists user(name varchar(128) primary key, created int(10))"
    cursor.execute(sql)

    #写入
    sql = "insert into user(name,created) values(%s,%s)"
    param = ("aaa",int(time.time()))
    n = cursor.execute(sql,param)
    print 'insert',n

    #写入多行
    sql = "insert into user(name,created) values(%s,%s)"
    param = (("bbb",int(time.time())), ("ccc",33), ("ddd",44) )
    n = cursor.executemany(sql,param)
    print 'insertmany',n

    #更新
    sql = "update user set name=%s where name='aaa'"
    param = ("zzz")
    n = cursor.execute(sql,param)
    print 'update',n

    #查询
    n = cursor.execute("select * from user")
    for row in cursor.fetchall():
    print row
    for r in row:
    print r

    #删除
    sql = "delete from user where name=%s"
    param =("bbb")
    n = cursor.execute(sql,param)
    print 'delete',n

    #查询
    n = cursor.execute("select * from user")
    print cursor.fetchall()

    cursor.close()

    #提交
    conn.commit()
    #关闭
    conn.close()

  • 相关阅读:
    记账本开发第一天-补
    20200418-补
    20200411-补
    20200404-补
    20200328-补
    暴力解N皇后
    nN皇后递归
    Hanoi汉诺塔非递归栈解
    Hanoi汉诺塔递归
    JMMjmm模型
  • 原文地址:https://www.cnblogs.com/noobkey/p/4635313.html
Copyright © 2020-2023  润新知