TypeError:插入mysql数据库时格式字符串的参数不足

时间:2015-10-18 08:44:07

标签: python mysql csv

我有一个像这样的csv文件:

nohaelprince@uwaterloo.ca, 01-05-2014
nohaelprince@uwaterloo.ca, 01-05-2014
nohaelprince@uwaterloo.ca, 01-05-2014
nohaelprince@gmail.com, 01-05-2014

我正在阅读上述csv文件并提取域名以及域名和日期的电子邮件地址数。我需要在MySQL表中插入所有这些东西,称为域。

以下代码中的错误为TypeError: not enough arguments for format string,并且在我尝试插入domains表时发生错误。

#!/usr/bin/python
import fileinput
import csv
import os
import sys
import time
import MySQLdb

from collections import defaultdict, Counter

domain_counts = defaultdict(Counter)

# ======================== Defined Functions ======================
def get_file_path(filename):
    currentdirpath = os.getcwd()  
    # get current working directory path
    filepath = os.path.join(currentdirpath, filename)
    return filepath
# ===========================================================
def read_CSV(filepath):

    with open('emails.csv') as f:
        reader = csv.reader(f)
        for row in reader:
            domain_counts[row[0].split('@')[1].strip()][row[1]] += 1

    db = MySQLdb.connect(host="localhost", # your host, usually localhost
                         user="root", # your username
                         passwd="abcdef1234", # your password
                         db="test") # name of the data base
    cur = db.cursor()

    q = """INSERT INTO domains(domain_name, cnt, date_of_entry) VALUES(%s, %s, STR_TO_DATE(%s, '%d-%m-%Y'))"""

    for domain, data in domain_counts.iteritems():
        for email_date, email_count in data.iteritems():
             cur.execute(q, (domain, email_count, email_date))

    db.commit()

# ======================= main program =======================================
path = get_file_path('emails.csv') 
read_CSV(path) # read the input file

我在做什么?

到目前为止,date_of_entry列的数据类型在MySQL中为date

3 个答案:

答案 0 :(得分:2)

你需要"%d-%m-%Y"在你的SQL语句中就是这样的。但python(或执行命令)首先尝试使用它进行字符串格式化并抛出此错误。

我认为你必须逃避它,你应该尝试以下:

q = """INSERT INTO domains(domain_name, cnt, date_of_entry) VALUES(%s, %s, STR_TO_DATE(%s, '%%d-%%m-%%Y'))"""

答案 1 :(得分:0)

试试这个:

q = """INSERT INTO domains(domain_name, cnt, date_of_entry) VALUES(%s, %s, STR_TO_DATE(%s, 'd-m-Y'))"""

所以我们已从STR_TO_DATE(%s, '%d-%m-%Y'))更改为STR_TO_DATE(%s, 'd-m-Y'))

答案 2 :(得分:0)

它将%s检测为格式字符串并且失败。你需要用引号包围它我猜

$mysqli->affected_rows;