python之資料庫操作,python資料庫

來源:互聯網
上載者:User

python之資料庫操作,python資料庫
資料庫操作

Python 操作 Mysql 模組的安裝

12345 linux:    yum install MySQL-python window:    http://files.cnblogs.com/files/wupeiqi/py-mysql-win.zip

SQL基本使用

1、資料庫操作

123 show databases; use [databasename];create database  [name];

2、資料表操作

12345678910 show tables; create table students    (        id int  not null auto_increment primary key,        name char(8) not null,        sex char(4) not null,        age tinyint unsigned not null,        tel char(13) null default "-"    );
1 CREATE TABLE `wb_blog` ( 2 `id` smallint(8) unsigned NOT NULL, 3 `catid` smallint(5) unsigned NOT NULL DEFAULT '0', 4 `title` varchar(80) NOT NULL DEFAULT '', 5 `content` text NOT NULL, 6 PRIMARY KEY (`id`), 7 UNIQUE KEY `catename` (`catid`) 8 ) ; 建立表wb_blog

3、資料操作

1234567 insert into students(name,sex,age,tel) values('alex','man',18,'151515151') delete from students where id =2; update students set name = 'sb' where id =1; select * from students

4、其他

123 主鍵外鍵左右串連

Python MySQL API

一、插入資料

123456789101112131415 import MySQLdb  conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',db='mydb')  cur = conn.cursor()  reCount = cur.execute('insert into UserInfo(Name,Address) values(%s,%s)',('alex','usa'))# reCount = cur.execute('insert into UserInfo(Name,Address) values(%(id)s, %(name)s)',{'id':12345,'name':'wupeiqi'})  conn.commit()  cur.close()conn.close()  print reCount
import MySQLdbconn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',db='mydb')cur = conn.cursor()li =[ ('alex','usa'), ('sb','usa'),]reCount = cur.executemany('insert into UserInfo(Name,Address) values(%s,%s)',li)conn.commit()cur.close()conn.close()print reCountMySQLdb

注意:cur.lastrowid

二、刪除資料

1234567891011121314 import MySQLdb conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',db='mydb') cur = conn.cursor() reCount = cur.execute('delete from UserInfo') conn.commit() cur.close()conn.close() print reCount

三、修改資料

12345678910111213 import MySQLdb conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',db='mydb') cur = conn.cursor() reCount = cur.execute('update UserInfo set Name = %s',('alin',)) conn.commit()cur.close()conn.close() print reCount

四、查資料

# ############################## fetchone/fetchmany(num)  ############################## import MySQLdb conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',db='mydb')cur = conn.cursor() reCount = cur.execute('select * from UserInfo') print cur.fetchone()print cur.fetchone()cur.scroll(-1,mode='relative')print cur.fetchone()print cur.fetchone()cur.scroll(0,mode='absolute')print cur.fetchone()print cur.fetchone() cur.close()conn.close() print reCount   # ############################## fetchall  ############################## import MySQLdb conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',db='mydb')#cur = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor)cur = conn.cursor() reCount = cur.execute('select Name,Address from UserInfo') nRet = cur.fetchall() cur.close()conn.close() print reCountprint nRetfor i in nRet:    print i[0],i[1]

 

   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.