Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python SQLITE3从表中选择布尔语句所在的列_Python_Sqlite - Fatal编程技术网

Python SQLITE3从表中选择布尔语句所在的列

Python SQLITE3从表中选择布尔语句所在的列,python,sqlite,Python,Sqlite,我尝试了sqlite3语句的3种不同变体来选择数据: cursor.execute('SELECT * FROM users WHERE username = ?', (username,)) cursor.execute('''SELECT * FROM users WHERE username = ?;''', (username,)) cursor.execute('SELECT * FROM users WHERE username = "monkey1" ')

我尝试了sqlite3语句的3种不同变体来选择数据:

    cursor.execute('SELECT * FROM users WHERE username = ?', (username,))
    cursor.execute('''SELECT * FROM users WHERE username = ?;''', (username,))
    cursor.execute('SELECT * FROM users WHERE username = "monkey1" ')
这些陈述的参考文献来自。然而,它们都不起作用。我怀疑我在做一些非常愚蠢的事情,但似乎无法理解这一点

我想能够打印出用户名“猴子”的数据。谢谢你帮我指出我愚蠢的错误

import sqlite3
import datetime


def get_user(connection, rows='all', username=None ):
    """Function to obtain data."""
    #create cursor object from sqlite connection object
    cursor = connection.cursor() 
    if rows == 'all':
        print("\nrows == 'all'")
        cursor.execute("SELECT * FROM users")
        data = cursor.fetchall()
        for row in data:
            print(row)

    if rows == 'one':
        print("\nrows == 'one'")
        cursor.execute('SELECT * FROM users WHERE username = ?', (username,))
        #cursor.execute('''SELECT * FROM users WHERE username = ?;''', (username,))
        #cursor.execute('SELECT * FROM users WHERE username = "monkey1" ')
        data = cursor.fetchone()
        print('data = ',data)

    cursor.close()
    return data



def main():
    database = ":memory:"

    table = """ CREATE TABLE IF NOT EXISTS users (
                    created_on  TEXT    NOT NULL UNIQUE,
                    username    TEXT    NOT NULL UNIQUE,
                    email       TEXT    NOT NULL UNIQUE
                 ); """

    created_on = datetime.datetime.now()
    username   = 'monkey'
    email      = 'monkey@gmail'

    created_on1 = datetime.datetime.now()
    username1   = 'monkey1'
    email1      = 'monkey1@gmail'

    # create a database connection & cursor
    conn = sqlite3.connect(database)
    cursor = conn.cursor() 

    # Insert data
    if conn is not None:
        # create user table
        cursor.execute(table)
        cursor.execute('INSERT INTO users VALUES(?,?,?)',(
            created_on, email, username))
        cursor.execute('INSERT INTO users VALUES(?,?,?)',(
            created_on1, email1, username1))
        conn.commit()
        cursor.close()
    else:
        print("Error! cannot create the database connection.")

    # Select data
    alldata = get_user(conn, rows='all')
    userdata = get_user(conn, rows='one', username=username )
    print('\nalldata = ', alldata)
    print('\nuserdata = ', userdata)
    conn.close()


main()

表定义中的字段顺序为
created\u on,username,email
,但您插入的数据为
created\u on,email,username
。因此,第一行的用户名是
monkey@gmail“

避免此类错误的一个好方法是在INSERT语句中指定列,而不是依赖于正确获取原始表定义的顺序:

INSERT INTO users (created_on, email, username) VALUES (?,?,?)

表定义中的字段顺序为
created\u on,username,email
,但您插入的数据为
created\u on,email,username
。因此,第一行的用户名是
monkey@gmail“

避免此类错误的一个好方法是在INSERT语句中指定列,而不是依赖于正确获取原始表定义的顺序:

INSERT INTO users (created_on, email, username) VALUES (?,?,?)

在进行select调用之前,我没有看到您在Python代码中实际为
username
赋值。我弄错了吗?在进行select调用之前,我没有看到您在Python代码中实际为
username
赋值。我弄错了吗?谢谢。完整地说,我将其改写为
cursor.execute(''INSERT INTO users(created_on,email,username)value(?,,?)'',(created_on,email,username))
。谢谢。完整地说,我将其改写为
cursor.execute(''INSERT INTO users(created_on,email,username)value(?,,?)'',(created_on,email,username))