Sqlite3在python中使用变量命名db文件

时间:2016-01-27 09:56:39

标签: python database python-3.x sqlite

如何使用当前日期命名我的db文件,以便在运行时创建一个以当前日期命名的db文件。这就是我到目前为止所做的:

import sqlite3
import time


timedbname = time.strftime("%d/%m/%Y")

# Connecting to the database file
conn = sqlite3.connect(???)

出现此错误与' /'相同或者' - '或'。'在"%d /%m /%Y":

conn = sqlite3.connect(timedbname, '.db')
TypeError: a float is required
27.01.2016

3 个答案:

答案 0 :(得分:0)

尝试使用:

time.strftime("%d-%m-%Y")

我猜它因为生成日期中的斜杠而无效。

答案 1 :(得分:0)

您的表名中不能有破折号。它也不能以数字开头。

import sqlite3
from datetime import date

timedbname = '_' + str(date.today()).replace('-','_')

# Connecting to the database file
conn = sqlite3.connect(':memory:')
cursor = conn.cursor()

cursor.execute('''CREATE TABLE %s (col1 int, col2 int)''' % (timedbname))
cursor.execute('''INSERT INTO %s VALUES (1, 2)''' % (timedbname))
cursor.execute('''SELECT * FROM %s'''%timedbname).fetchall()

答案 2 :(得分:0)

这有效:

my.list <- list()
my.list[[name_arr[1]]] <- value_arr[1:3]
相关问题