Pycharm3.5操作mysql


python3.5 操作mysql的模塊不是MySQLdb了,而是pymysql

下載安裝pymysql

可以使用瀏覽器或者迅雷下載,或者干脆再linux上,wget下載然后再傳到windows上

https://pypi.python.org/packages/source/P/PyMySQL3/PyMySQL3-0.5.tar.gz

解壓之后

 

運行cmd,切換到PyMySQL3-0.5目錄,執行如下命令安裝此插件

python setup.py install

 

安裝完畢,重新打開pycharm軟件

把sql語句放在一個文件中

 

然后寫一個python腳本連接數據庫,執行上面語句

其中mysql庫名為dbuser,連接此測試庫的用戶名和密碼都是dbuser,此表是從測試庫上拖下來的,自己測試機器更新完沒問題,再導入到測試庫

逐行執行每條update語句

#Auther nmap
#coding=utf-8
import pymysql
conn = pymysql.connect(host="10.0.2.17",port=3306,user="dbuser",passwd="dbuser",db="dbuser",charset="utf8")
cur = conn.cursor()
f_sql=open('update','r',encoding='utf-8')
for line in f_sql:
    sql = line
    cur.execute(sql)

  

這次要更新的字段是c_nickname  它是昵稱。

下面賬號都是游戲的機器人賬號,改成中文的話更加真實點

下圖為更新完畢的效果,昵稱這里默認是和c_numbercode一致,這里都改成中文的了

 

 

 之所以使用python去操作數據庫,而不是直接在mysql命令行執行更新,是自己遇到一個一時無法解決的小問題。

在mysql命令行批量更新時,執行總是報錯,而且即使成功的更新,插入之后也變成亂碼了。

比如下面sql語句

update tab_user set c_nickname='漢芬菲'  where c_numbercode='50001970';
update tab_user set c_nickname='連世英'  where c_numbercode='50001971';
update tab_user set c_nickname='言心'  where c_numbercode like "50001972";
update tab_user set c_nickname='靜逸'  where c_numbercode like "50001973";
update tab_user set c_nickname='hehe'  where c_numbercode='50001974';
update tab_user set c_nickname='厚孤晴'  where c_numbercode='50001975';
update tab_user set c_nickname='厚孤晴'  where c_numbercode='50001975';
update tab_user set c_nickname='初蝶'  where c_numbercode='50001976';
update tab_user set c_nickname='夏青'  where c_numbercode='50001977';
update tab_user set c_nickname='沃德'  where c_numbercode='50001978';
update tab_user set c_nickname='軒轅'  where c_numbercode='50001979';
update tab_user set c_nickname='痴梅'  where c_numbercode='50001980';
update tab_user set c_nickname='9527'  where c_numbercode='50001981';
update tab_user set c_nickname='莫憶楓'  where c_numbercode='50001982';
update tab_user set c_nickname='申屠'  where c_numbercode='50001983';
update tab_user set c_nickname='甄夢'  where c_numbercode='50001984';

  

 在mysql命令行執行更新時,如下

報錯情況如下,總是提示語法錯誤。但是每條sql語句單獨拿出來又能執行成功,而且不會亂碼,奇怪

mysql> update tab_user set c_nickname='漢芬菲'  where c_numbercode='50001970';
update tab_user set c_nickname='連世英'  where c_numbercode='50001971';
update tab_user set c_nickname='言心'  where c_numbercode like "50001972";
update tab_user set c_nickname='靜逸'  where c_numbercode like "50001973";
update tab_user set c_nickname='hehe'  where c_numbercode='50001974';
update tab_user set c_nickname='厚孤晴'  where c_numbercode='50001975';
update tab_user set c_nickname='厚孤晴'  where c_numbercode='50001975';
update tab_user set c_nickname='初蝶'  where c_numbercode='50001976';
update tab_user set c_nickname='夏青'  where c_numbercode='50001977';
update tab_user set c_nickname='沃德'  where c_numbercode='50001978';
update tab_user set c_nickname='軒轅'  where c_numbercode='50001979';
update tab_user set c_nickname='痴梅'  where c_numbercode='50001980';
update tab_user set c_nickname='9527'  where c_numbercode='50001981';
update tab_user set c_nickname='莫憶楓'  where c_numbercode='50001982';
update tab_user set c_nickname='申屠'  where c_numbercode='50001983';
update tab_user set c_nickname='甄夢'  where c_numbercode='50001984';
Database changed
Rows matched: 1  Changed: 0  Warnings: 0
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '50001971'' at line 1
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''璦€蹇?  where c_numbercode like "50001972"' at line 1
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''闈欓€?  where c_numbercode like "50001973"' at line 1
Database changed
Rows matched: 1  Changed: 0  Warnings: 0
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '50001975'' at line 1
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '50001975'' at line 1
Database changed
Rows matched: 1  Changed: 1  Warnings: 0
Database changed
Rows matched: 1  Changed: 1  Warnings: 0
Database changed
Rows matched: 1  Changed: 1  Warnings: 0
Database changed
Rows matched: 1  Changed: 1  Warnings: 0
1366 - Incorrect string value: '\xEE\x95\xAA' for column 'c_nickname' at row 1
Database changed
Rows matched: 1  Changed: 0  Warnings: 0
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '50001982'' at line 1
Database changed
Rows matched: 1  Changed: 1  Warnings: 0
Database changed
Rows matched: 1  Changed: 1  Warnings: 0
mysql>   

 下面是通過Navicat工具執行更新時,數據庫亂碼的情況

 

最后采用python去操作數據庫,一切正常

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM