So cute are you python 14

来源:互联网 发布:凸优化 求解 编辑:程序博客网 时间:2024/05/21 07:13
1.ubuntu 系统 安装mysql 模块直接sudo apt-get install python-mysqldb如果编译需要 sudo apt-get install libmysqlclient-dev

2.简单连接数据库:

代码:

#!/usr/bin/evn python#coding:utf-8#FileName:mysql_op01.py#Function:just simply conneced to mysql database#History:28-10-2013import MySQLdbdef conn(self):    try:        conn=MySQLdb.connect(host='localhost',user='root',passwd='yxh1234yxh!@#$',db='test',port=3306)        cur=conn.cursor()        cur.execute('select * from mytest_user')        print 'Connected to mysql ,OK!'        print 'And then you could do anything that you want...\r\n'        cur.close()        conn.close()    except MySQLdb.Error,e:        print 'MySQL Error %d:%s'%(e.args[0],e.args[1])if __name__=='__main__':    conn('A')


结果:

$ python mysql_op01.py Connected to mysql ,OK!And then you could do anything that you want...

3.对数据库进行简单的操作:

代码:

#!/usr/bin/evn python#coding:utf-8#FileName:mysql_op02.py#Function:This part will show you simething that using mysql db.#History:28-10-2013import MySQLdbclass Connect:    global conn;    def __init__(self,host,user,pwd,port):        self.host=host        self.user=user        self.pwd=pwd        self.port=port    def conn(self):        try:            conn=MySQLdb.connect(host=self.host,user=self.user,passwd=self.pwd,port=self.port)            return conn        except MySQLdb.Error,e:            print "Mysql Error %d: %s" % (e.args[0], e.args[1])    def __insert__(self,_id,_info):        try:            values=[int(_id),str(_info)]            conn1=conn(self)            cur=conn1.cursor()            conn1.select_db('python')            cur.execute('insert into test values(%s,%s)',values)                        conn1.commit()            conn1.close()            cur.close()            print 'Insert succese!!!\n'        except MySQLdb.Error,e:            print e                             def __show__(self):        try:            conn2=conn(self)            cur=conn2.cursor()            conn2.select_db('python')            cur.execute('select * from test')        #cur=conn1.cursor()            result=cur.fetchall()            for r in result:            #print r                print 'ID:%s info:%s \r\n'%r            conn2.close()            cur.close()        except MySQLdb.Error,e:            print e         def run(self):        print 'Starting to connected to the mysql db..\r\n'        global conn,cur;        try:            conn=MySQLdb.connect(host=self.host,user=self.user,passwd=self.pwd,port=self.port)            cur=conn.cursor()            #create an db named python            #cur.execute('create database if not exists python')            conn.select_db('python')            #cur.execute('create table test(id int,info varchar(25))')            #create an table named test            print 'DB was created!!!\n'            values=[1,'student was very hard!!!']            #Insert an value to this table            #cur.execute('insert into test values(%s,%s)',values)            #Do the ending.                        conn.commit()            cur.close()            conn.close()        except MySQLdb.Error,e:            print "Mysql Error %d: %s" % (e.args[0], e.args[1])if __name__=='__main__':    c=Connect('localhost','root','yxh1234yxh!@#$',3306)    #c.run()    c.__show__()    _id=raw_input('You ought to input a number:_')    _info=raw_input('You ought to input info str:_')    c.__insert__(_id,_info)    c.__show__()


结果:

ID:12 info:lllllll You ought to input a number:_3You ought to input info str:_yyyoooInsert succese!!!ID:1 info:student was very hard!!! ID:1 info:student was very hard!!! ID:2 info:you should ID:9 info:wwwwwwwww ID:10 info:ooooooo ID:11 info:qqqqq ID:12 info:lllllll ID:3 info:yyyooo 
只是对mysql 进行一些简单的操作,更复杂的在后面。


原创粉丝点击