我有一个Python类:
class Database:
conn = sqlite3.connect(‘database.db’)
c = conn.cursor()
def __init__(self):
pass
在此类中,我有多种方法将与数据库类一起使用,例如:
def create_table(self, table_name, *args):
pass
def add_user(self):
pass
def remove_user(self):
pass
依此类推。我的问题是:如果不确定要包含多少列,如何在我的“ create_table”函数中使用* args。例如,我知道我将拥有第一列,最后列和付款列,然后我的函数将如下所示:
def create_table(self, table_name, *args): c.execute("""CREATE TABLE ‘{}’ (‘{}’ text, ‘{}’ text, ‘{}’ integer).format(self.table_name, self.first, self.last, self.pay)”””) c.commit()
因此,如果我想创建表,我可以这样做:
Item = Database() Item.create_table('employees', ‘First_name’, ’Last_name’, 100000)
但是如果我不知道会有多少列怎么办?谢谢
我有一个Python类:类数据库:conn = sqlite3.connect('database.db')c = conn.cursor()def __init __(self):pass在该类中,我有多种方法,我将.. 。
def create_table(self, tableName, *args,):
columns = ''
for i in args:
columns += i
columns += ' '
message = '"""CREATE TABLE {} ({})"""'.format(tableName, columns[:-1])
return message
不确定列的可变性;或时间范围。似乎您有一组基本定义,然后稍后弹出一个新列。因此,假设您的表以上面提到的4开始。