Python操作MySQL數(shù)據(jù)庫(kù)的示例代碼
1. MySQL Connector
1.1 創(chuàng)建連接
import mysql.connector config={ 'host':'localhost','port':'3306', 'user':'root','password':'password', 'database':'demo' } con=mysql.connector.connect(**config) import mysql.connector config={ 'host':'localhost','port':'3306', 'user':'root','password':'password', 'database':'demo' } con=mysql.connector.connect(**config)
1.2 Cursor
import mysql.connector con=mysql.connector.connect( host='localhost',port='3306', user='root',password='password', database='demo' ) cursor=con.cursor() sql='SELECT empno,job,sal FROM t_bonus;' cursor.execute(sql) print(type(cursor)) for i in cursor: print(i) con.close() Result: <class ’mysql.connector.cursor_cext.CMySQLCursor’> (7369, ’CLERK’, Decimal(’8000.00’)) (7499, ’SALESMAN’, Decimal(’1600.00’)) (7521, ’SALESMAN’, Decimal(’1250.00’)) (7566, ’MANAGER’, Decimal(’2975.00’)) (7654, ’SALESMAN’, Decimal(’1250.00’)) (7698, ’MANAGER’, Decimal(’2850.00’)) (7782, ’MANAGER’, Decimal(’2450.00’)) (7788, ’ANALYST’, Decimal(’3000.00’)) (7839, ’PRESIDENT’, Decimal(’5000.00’)) (7844, ’SALESMAN’, Decimal(’1500.00’)) (7900, ’CLERK’, Decimal(’950.00’)) (7902, ’ANALYST’, Decimal(’3000.00’)) (7934, ’CLERK’, Decimal(’1300.00’))
1.3 SQL注入攻擊
username=1 OR 1=1 password=1 OR 1=1 在使用字符串直接拼接時(shí)OR之前不管對(duì)錯(cuò),與OR結(jié)合都為true 解決方法——預(yù)編譯(也可以提高速度)1.4 事務(wù)管理和異常處理
sql連接和使用異常處理異常
import mysql.connector try: con=mysql.connector.connect( host='localhost',port='3306', user='root',password='password', database='demo' ) con.start_transaction() cursor=con.cursor() sql='INSERT INTO t_dept(deptno,dname,loc) VALUES(%s,%s,%s);' cursor.execute(sql,(60,'SALES','HUBAI')) con.commit() except Exception as e: if 'con' in dir(): con.rollback() print(e) finally: if 'con' in dir(): con.close()
1.5 刪除數(shù)據(jù)
import mysql.connector,mysql.connector.pooling config={ 'host': 'localhost', 'port': '3306', 'user': 'root', 'password': 'password', 'database': 'demo' } try: pool=mysql.connector.pooling.MySQLConnectionPool(**config,pool_size=5) con=pool.get_connection() con.start_transaction() cursor = con.cursor() sql = 'DELETE FROM t_dept WHERE deptno=%s' cursor.execute(sql, (70,)) con.commit() except Exception as e: if 'con' in dir(): con.rollback() print(e) # do not need to close con
executemany() 反復(fù)執(zhí)行一條SQL語(yǔ)句
import mysql.connector,mysql.connector.pooling config={ 'host': 'localhost', 'port': '3306', 'user': 'root', 'password': 'password', 'database': 'demo' } try: pool=mysql.connector.pooling.MySQLConnectionPool(**config,pool_size=5) con=pool.get_connection() con.start_transaction() cursor = con.cursor() sql = 'INSERT INTO t_dept(deptno,dname,loc) VALUES(%s,%s,%s);' date=[[70,'SALES','BEIJING'],[80,'ACTOR','SHANGHAI']] cursor.executemany(sql, date) con.commit() except Exception as e: if 'con' in dir(): con.rollback() print(e) # do not need to close con
2. 數(shù)據(jù)庫(kù)連接池
數(shù)據(jù)庫(kù)的連接是昂貴的,一個(gè)連接要經(jīng)過(guò)TCP三次握手,四次揮手,而且一臺(tái)計(jì)算機(jī)的最大線程數(shù)也是有限的 數(shù)據(jù)庫(kù)連接池技術(shù)就是先創(chuàng)建好連接,再直接拿出來(lái)使用import mysql.connector,mysql.connector.pooling config={ 'host': 'localhost', 'port': '3306', 'user': 'root', 'password': 'password', 'database': 'demo' } try: pool=mysql.connector.pooling.MySQLConnectionPool(**config,pool_size=5) con=pool.get_connection() con.start_transaction() cursor = con.cursor() sql = 'INSERT INTO t_dept(deptno,dname,loc) VALUES(%s,%s,%s);' cursor.execute(sql, (70, 'SALES', 'HUBAI')) con.commit() except Exception as e: if 'con' in dir(): con.rollback() print(e) # do not need to close con
以上就是Python操作MySQL數(shù)據(jù)庫(kù)的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Python操作MySQL數(shù)據(jù)庫(kù)的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 三道java新手入門(mén)面試題,通往自由的道路--鎖+Volatile2. AJAX實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)效果3. SpringBoot+SpringCache實(shí)現(xiàn)兩級(jí)緩存(Redis+Caffeine)4. ASP.NET MVC視圖頁(yè)使用jQuery傳遞異步數(shù)據(jù)的幾種方式詳解5. php中加密解密DES類的簡(jiǎn)單使用方法示例6. php讀取xml中某個(gè)元素的內(nèi)容(PHP5以上才支持)7. Java基于redis和mysql實(shí)現(xiàn)簡(jiǎn)單的秒殺(附demo)8. 如何用python識(shí)別滑塊驗(yàn)證碼中的缺口9. 關(guān)于HTML5的img標(biāo)簽10. 關(guān)于JSP用戶登錄連接數(shù)據(jù)庫(kù)詳情
