L
L
Lera Kruk2021-08-17 15:41:43
Python
Lera Kruk, 2021-08-17 15:41:43

Hello. What is the correct way to write a function to determine the data type when creating a table in sqlite3?

data_type = {
'str': 'TEXT',
'int':'INTEGER',
'float':'REAL',
'nul':'NUL'
}
@staticmethod
def create_table(dbname, table, row):
import sqlite3
conn = sqlite3.connect(dbname)
cur = conn.cursor()
cur.execute(f"CREATE TABLE IF NOT EXISTS {table}({', '.join([str(i) for i in row])}:[ {data_type} for i in row] ")
conn.commit()
how to write a data type definition in a table?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-08-17
@Vindicar

Well, for starters, do not try to shove the request into one f-line, but collect it in parts. It's unreadable. And why do you need @staticmethod if it's not part of the class? And if it's part of a class, then why staticmethod and not classmethod - you're referring to the data_type constant, which should be in the same class, aren't you?
In general, I have a feeling that you are trying to make your own ORM. Try ponyorm or peewee, they are simple and comfortable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question