Python中的SQLITE不是创建表而是创建数据库 [英] SQLITE in Python not creating tables but creates database

查看:253
本文介绍了Python中的SQLITE不是创建表而是创建数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试连接到SQLITE数据库,看起来代码确实创建了数据库,但它不会创建表,也不会在其中插入任何内容(很可能是因为它们从未创建过)



我有以下代码:

I am trying to connect to SQLITE database and it would seem that the code does create the database, but however it does not create the tables nor does it insert anything in them (most probably because they were never created)

I have the following code:

import sqlite3
conn = sqlite3.connect("dfg.db")
c = conn.cursor()

def create_tabe():
    c.execute('CREATE TABLE IF NOT EXISTS tabl(city TEXT, temp REAL)')

def data_entry():
    c.execute("INSERT INTO tabl VALUES ('dasfsd', 32434)")
    conn.commit()
    c.close()
    conn.close()





我尝试过:



我有SQLite数据库浏览器,在其中我打开数据库但缺少任何表格或任何东西。另外,我在项目旁边的文件夹中看到了我的数据库。



What I have tried:

I have SQLite DB Browser and in it I open the database but lack any tables or anything at all. Also I see my database in the folder next to my project.

推荐答案

import sqlite3
conn = sqlite3.connect("dfg.db")
c = conn.cursor()
 
def create_table():
    c.execute('CREATE TABLE IF NOT EXISTS tabl(city TEXT, temp REAL)')
 
def data_entry():
    c.execute("INSERT INTO tabl VALUES ('dasfsd', 32434)")
    conn.commit()
    c.close()
    conn.close()


#Add this to create a table
create_table()





好​​像你忘记了调用这个函数

注意我只调用了创建表的函数调用第二个函数定义来添加数据



seems to be you have forgotten to call the function
Note I did called only the function which creates the table call the second function definition to add the data


这篇关于Python中的SQLITE不是创建表而是创建数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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