简体   繁体   English

将SQLite3与python结合使用

[英]Using SQLite3 with python

I just started learning SQLite3 for python today, and I'm having trouble figuring out why this won't work. 我今天才刚刚开始学习python的SQLite3,但是我很难弄清为什么它不起作用。

import sqlite3, os

if not os.path.isfile("G:\\Python\\My first database.db"):
    dtabse = sqlite3.connect("G:\\Python\\My first database.db")
    cursr = dtabse.cursor()

    cursr.execute("""CREATE TABLE Students
    (first_name text,
    surname text,
    DOB text,
    Form text)
    """)

    cursr.execute(""" INSERT INTO Students
    VALUES ("Dave", "Edwards", "16", "11AB")""")

    dtabse.commit()
    dtabse.close()
else:
    dtabse = sqlite3.connect("G:\\Python\\My first database.db")
    cursr = dtabse.cursor()

    print(cursr.fetchall())

In a powerpoint I was viewing, it said fetchall() should retrieve everything and display it. 在我正在查看的简报中,它说fetchall()应该检索所有内容并显示它。 On the first go of this program it won't find a file in this directory, so the if area gets executed. 在该程序的第一步中,它不会在此目录中找到文件,因此将执行if区域。 When I next run the program the else area gets executed. 当我下次运行程序时,else区域将被执行。

That much works, on the first go the program ends and begins. 这么多的工作,程序首先结束并开始。 On the second go it prints an empty list, when I was expecting the table. 第二步,当我期望该表时,它会打印一个空列表。 I checked the database file and the data is there, so why can't I print it? 我检查了数据库文件,并且数据在那里,所以为什么不能打印它?

您需要一个SELECT语句来检索所需的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM