如何使用存储在变量中的值创建或打开数据库 [英] How to create or open a database with a value stored in a variable

查看:25
本文介绍了如何使用存储在变量中的值创建或打开数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using sqlite to store some values in a database , but the values are from different source so i have to put them in different database so i tried something like:

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

但它没有用,任何解决方案或想法.

but it didn't work, any solutions or ideas.

报告的错误是:

引号内的%s :

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

%s 不带引号:

 SyntaxError: invalid syntax

推荐答案

str.__mod__()str 上的一个方法:

str.__mod__() is a method on str:

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

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

Or because fullpath is already a string:

db = sqlite3.connect(fullpath)

这篇关于如何使用存储在变量中的值创建或打开数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆