!/usr/bin/envpythoncoding:utf-8importmysqldbdefdb_execute(sql):dbs=mysqldb.connect(host='localhost',user='root',passwd='root',db='db',port=3306)cursor=dbs.cursor()cursor....
!/usr/bin/env python
coding: utf-8
import mysqldb
def db_execute(sql):
dbs = mysqldb.connect(host='localhost', user='root', passwd='root', db='db', port=3306)
cursor = dbs.cursor()
cursor.execute(sql)
cursor.close()
def read_file(file_path):
sql_lines =
with open(file_path, 'r') as file:
for line in file.readlines():
sql = 'insert into ips (ip,ipint,city) values("",inet_aton("")," ");'.format(line)
return '\r\n'.join(sql_lines)
sql_lines = read_file('c:\a.txt')
db_execute(sql_lines)
排除存在問題步驟如下:
第一:引入的import mysqldb 沒有報錯.同時使用如下**測試正常:
第二:生成的insert into 語句在mysql資料庫可以正常插入,但是目前提示錯誤資訊如下:
insert into ips (ip,ipint,city) values("113.236.197.57",inet_aton("113.236.197.5
7")," ");
traceback (most recent call last):
file "new044.py", line 24, in
db_execute(sql_lines)
file "new044.py", line 7, in db_execute
cursor.execute(sql)
file "c:python27libsite-packagesmysqldbcursors.py", line 202, in execute
self.errorhandler(self, exc, value)
file "c:python27libsite-packagesmysqldbconnections.py", line 36, in defau
lterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.operationalerror: (1048, "column 'ipint' cannot be null")
展開
es 批量匯入檔案
首先是json格式的檔案 curl xpost localhost 9200 bank account bulk?pretty data binary accounts.json 1 需要在accounts.json所在的目錄執行curl命令。2 localhost 9200是es得訪問位址和埠 3...
python檔案匯入 python 檔案匯入
基本匯入 import time 呼叫的時候 time.sleep 3 匯入包裡某個方法 from time import sleep 呼叫的時候 sleep 3 區別import time和from time import sleep 兩種方法都可以成功匯入,但不同的匯入方式,呼叫的方式也不同。如...
如何將matlab的 mat檔案匯入python
雖然是乙個簡單的問題,但是還是花了些時間!第一步 讀取.mat檔案 import scipy.io as sio matfn u d hog svm data global hog features.mat dataset sio.loadmat matfn dataset 但是這樣讀取得到的是乙個...