zoukankan      html  css  js  c++  java
  • python数据库操作

    1.简单例子

    #-*-coding:UTF-8-*-
    import MySQLdb
    db=MySQLdb.connect(host='localhost',user='root',passwd='root')  #成功则返回一个连接对象
    cur=db.cursor()		#创建一个光标来执行sql语句
    cur.execute('select version()') #执行SQL语句
    row=cur.fetchone()	#得到一个结果元组
    print 'server version:' ,row[0]
    cur.close()
    db.close()


    2.例子二

    #-*-coding:UTF-8-*-
    import MySQLdb
    import sys
    db=MySQLdb.connect(host='localhost',user='root',passwd='root',db='chen')  #成功则返回一个连接对象
    cursor=db.cursor()
    cursor.execute('drop table if exists animal')
    cursor.execute('create table animal(name   char(40),category char(40))')
    cursor.execute("insert into animal(name,category) values ('snake','reptile'),('frog','amphibian')")
    cursor.execute ("SELECT name, category FROM animal")
    while (1):
    	row = cursor.fetchone()
    	if row == None:
    		 break
    	print "%s, %s" % (row[0], row[1])
    print "Number of rows returned: %d" % cursor.rowcount
    db.commit()
    db.close()


     

  • 相关阅读:
    猜数字游戏(补)
    团队项目五(项目回顾)
    项目回顾
    第二次阶段冲刺
    团队项目(任务三):第一次冲刺
    个人项目(一):新猜数字
    课后作业(一)
    团队任务二
    团队任务(一)
    课后作业(一)
  • 原文地址:https://www.cnblogs.com/chenjianhong/p/4144814.html
Copyright © 2011-2022 走看看