python mysql data change and query
来源:互联网 发布:编程大括号 编辑:程序博客网 时间:2024/06/06 19:34
首先下载MySQLdb 安装
python连接mysql,进行数据库表变更和查询:
python mysql insert delete query:
#!/usr/bin/pythonimport MySQLdbdef doInsert(cursor,db): #insert # Prepare SQL query to INSERT a record into the database. sql = "UPDATE EMPLOYEE SET AGE = AGE+1 WHERE SEX = '%c'" %('M') try: cursor.execute(sql) db.commit() except: db.rollback()def do_query(cursor,db): sql = "SELECT * FROM EMPLOYEE \ WHERE INCOME > '%d'" % (1000) try: # Execute the SQL command cursor.execute(sql) # Fetch all the rows in a list of lists. results = cursor.fetchall() print 'resuts',cursor.rowcount for row in results: fname = row[0] lname = row[1] age = row[2] sex = row[3] income = row[4] # Now print fetched result print "fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \ (fname, lname, age, sex, income ) except: print "Error: unable to fecth data"def do_delete(cursor,db): sql = 'DELETE FROM EMPLOYEE WHERE AGE > {}'.format(20) try: cursor.execute(sql) db.commit() except: db.rollback()def do_insert(cursor,db,firstname,lastname,age,sex,income): sql = "INSERT INTO EMPLOYEE(FIRST_NAME, \ LAST_NAME, AGE, SEX, INCOME) \ VALUES ('%s', '%s', '%d', '%c', '%d' )" % \ (firstname,lastname,age,sex,income) try: cursor.execute(sql) db.commit() except: db.rollback() # Open database connection# change this to your mysql account#connect(server,username,password,db_name)db = MySQLdb.connect("localhost","root","root","pydb" )# prepare a cursor object using cursor() methodcursor = db.cursor()do_query(cursor,db)doInsert(cursor,db)do_query(cursor,db)do_delete(cursor,db)do_query(cursor,db)do_insert(cursor,db,'hunter','xue',22,'M',2000)do_insert(cursor,db,'mary','yang',22,'f',5555)do_insert(cursor,db,'zhang','xue',32,'M',5000)do_insert(cursor,db,'hunter','xue',22,'M',333)do_query(cursor,db)# disconnect from serverdb.close()
之后可以在此基础上根据需要进行封装。
- python mysql data change and query
- Change mysql default data location
- SQL Server feature:Change Data Capture(CDC) and Change Tracking
- Using Presto to combine data from Hive and MySQL in one SQL-like query
- Data Type Choices and Query Efficiency
- mysql change table charset and collation
- CC2540/2541 : Change Advertising Data And Scan Response Without Rebooting.
- python mysql update data
- Mysql binlog and The General Query Log
- processing-MySQL learning1-getString() , getInt() and query()
- Automatic Web Query Classification Using Labeled and Unlabeled Training Data
- Spring Data Redis(Repositories-Queries and Query Methods)
- How to change mysql default data directory in Ubuntu
- How to Change default MySQL Data Directory in Linux111
- mysql OUTFILE and LOAD DATA
- query mysql using pymysql in Python 3.3
- [Python for Data Analysis]Python Basic2--Data Structure and Sequences
- Data access between different DBMS and other txt/csv data source by DB Query Analyzer
- Win32汇编开发_MASM32
- 一步一步学android之布局管理器——LinearLayout
- Js获取当前日期时间及其它操作
- gridview中checkbox编辑
- ZOJ 3562 Alice's Sequence I
- python mysql data change and query
- ios日期格式转换
- 使用JS和使用Jquery向select下添加option的区别
- 原 教 旨 儒 家 论
- poj 2429 GCD & LCM Inverse
- jquery qtip demo
- 关于Linux静态库和动态库的分析
- 判断某一点在直线左右侧的方法...
- Poj 1523 SPF 关节点