Python 3 + SQLite 检查
Python 3 + SQLite check
你好
我可能对 SQLite 函数有疑问。
所以,问题:
如何查看我在Python中设置的name是否在某列?
示例:
name = 'John'
Table name = my_table
Column name = users
代码详情:
C = conn.cursor()
请
根据需要在查询中使用参数。请参阅随附的示例以更好地理解。
用于在 tables
中搜索值的示例 SQLite 代码
import sqlite3 as sqlite
import sys
conn = sqlite.connect("test.db")
def insert_single_row(name, age):
try:
age = str(age)
with conn:
cursor = conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS USER_TABLE(NAME TEXT, AGE INTEGER);")
cursor.execute("INSERT INTO USER_TABLE(NAME, AGE) VALUES ('"+name+"',"+age+")")
return cursor.lastrowid
except:
raise ValueError('Error occurred in insert_single_row(name, age)')
def get_parameterized_row(name):
try:
with conn:
cursor = conn.cursor()
cursor.execute("SELECT * FROM USER_TABLE WHERE NAME = :NAME",
{"NAME":name})
conn.commit()
return cursor.fetchall()
except:
raise ValueError('Error occurred in get_parameterized_row(name)')
if __name__ == '__main__':
try:
return_id = insert_single_row("Shovon", 24)
return_id = insert_single_row("Shovon", 23)
return_id = insert_single_row("Sho", 24)
all_row = get_parameterized_row("Shovon")
for row in all_row:
print(row)
except Exception as e:
print(str(e))
输出:
('Shovon', 24)
('Shovon', 23)
我在这里创建了一个名为 USER_TABLE
的 table,它具有两个属性:NAME
和 AGE
。然后我在 table 中插入了几个值并搜索了特定的 NAME
。希望它提供了一种在项目中开始使用 SQLite 的方法。
你好 我可能对 SQLite 函数有疑问。
所以,问题:
如何查看我在Python中设置的name是否在某列?
示例:
name = 'John'
Table name = my_table
Column name = users
代码详情:
C = conn.cursor()
请
根据需要在查询中使用参数。请参阅随附的示例以更好地理解。
用于在 tables
中搜索值的示例 SQLite 代码import sqlite3 as sqlite
import sys
conn = sqlite.connect("test.db")
def insert_single_row(name, age):
try:
age = str(age)
with conn:
cursor = conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS USER_TABLE(NAME TEXT, AGE INTEGER);")
cursor.execute("INSERT INTO USER_TABLE(NAME, AGE) VALUES ('"+name+"',"+age+")")
return cursor.lastrowid
except:
raise ValueError('Error occurred in insert_single_row(name, age)')
def get_parameterized_row(name):
try:
with conn:
cursor = conn.cursor()
cursor.execute("SELECT * FROM USER_TABLE WHERE NAME = :NAME",
{"NAME":name})
conn.commit()
return cursor.fetchall()
except:
raise ValueError('Error occurred in get_parameterized_row(name)')
if __name__ == '__main__':
try:
return_id = insert_single_row("Shovon", 24)
return_id = insert_single_row("Shovon", 23)
return_id = insert_single_row("Sho", 24)
all_row = get_parameterized_row("Shovon")
for row in all_row:
print(row)
except Exception as e:
print(str(e))
输出:
('Shovon', 24)
('Shovon', 23)
我在这里创建了一个名为 USER_TABLE
的 table,它具有两个属性:NAME
和 AGE
。然后我在 table 中插入了几个值并搜索了特定的 NAME
。希望它提供了一种在项目中开始使用 SQLite 的方法。