如何使用存储在变量

时间:2015-06-11 22:11:23

标签: python sqlite

我正在使用sqlite在数据库中存储一些值,但是值来自不同的源,因此我必须将它们放在不同的数据库中,所以我尝试了类似的东西:

 source_name = "hello"
 ext = ".db"
 path = "d:/"
 fullpath = path + source_name + ext
 db = sqlite3.connect("%s") % (fullpath)

但它不起作用,任何解决方案或想法。

报告的错误是:

引号内的

%s:

 TypeError: unsupported operand type(s) for %: 'sqlite3.Connection' and 'str'

%s没有引号:

 SyntaxError: invalid syntax

1 个答案:

答案 0 :(得分:3)

str.__mod__()str上的方法:

db = sqlite3.connect("%s" % fullpath)

或者因为fullpath已经是一个字符串:

db = sqlite3.connect(fullpath)