在Python SQLite中删除行时出现问题[重复]

hlswsv35  于 2022-11-14  发布在  SQLite
关注(0)|答案(1)|浏览(145)

这个问题这里已经有答案了

Unfindable SQLite Syntax Error(1个应答)
19天前关门了。

import pandas as pd
import sqlite3 as sql
import os
sea_level_df = pd.read_csv(r"C:\Users\slaye\OneDrive\Desktop\SeaLevel.csv", skiprows=3)
display(sea_level_df)
conn = sql.connect(r"C:\Users\slaye\OneDrive\Desktop\sqlite\db\z.db")
cur = conn.cursor()
s = sea_level_df.to_sql('v', conn)
cur.commit(s)
conn.close()

class database:
    
    def __init__(self):
        conn = sql.connect('z')
        cur = conn.cursor()
        
    def create_table():
        conn = sql.connect('z')
        cur = conn.cursor()
        n = """CREATE TABLE IF NOT EXISTS k( 
        id INTEGER PRIMARY KEY,
        year INTEGER NOT NULL
        sea_level INTEGER NOT NULL)
        ;"""
        cur.execute(n)
        conn.commit()
        print("table created")
        conn.close()
            
    def insert():
        conn = sql.connect(r"C:\Users\slaye\OneDrive\Desktop\sqlite\db\z.db")
        cur = conn.cursor()
        query = ("""INSERT INTO v VALUES (1223, 2022, -17.89, 0, 0, 0);""")
        cur.execute(query)
        conn.commit()
        t = ("""SELECT * FROM v;""")
        w = cur.execute(t).fetchall()
        print(w)
        conn.close()
              
    def remove():
        conn = sql.connect(r"C:\Users\slaye\OneDrive\Desktop\sqlite\db\z.db")
        cur = conn.cursor()
        g = ("""DELETE FROM v
        WHERE index = 1221;""")
        cur.execute(g)
        conn.commit()
        b = ("""SELECT * FROM v;""")
        q = cur.execute(b).fetchall()
        print(q)
        conn.close()
        
    def search():
        conn = sql.connect(r"C:\Users\slaye\OneDrive\Desktop\sqlite\db\z.db")
        cur = conn.cursor()
        x = ("""SELECT * FROM v WHERE index = 1221;""")
        h = cur.execute(x).fetchall()
        print(h)
        conn.close()
        
    def test():
        conn = sql.connect('z')
        cur = conn.cursor()
        u = cur.execute("""SELECT * FROM z;""").fetchall()
        print(u)

所以我在使用标题为“删除”和“搜索”的功能时遇到了麻烦。我分别得到了以下错误:

OperationalError                          Traceback (most recent call last)
Input In [76], in <cell line: 1>()
----> 1 database.remove()

Input In [75], in database.remove()
     33 cur = conn.cursor()
     34 g = ("""DELETE FROM v
     35 WHERE index = 1221;""")
---> 36 cur.execute(g)
     37 conn.commit()
     38 b = ("""SELECT * FROM v;""")

OperationalError: near "index": syntax error
OperationalError                          Traceback (most recent call last)
Input In [77], in <cell line: 1>()
----> 1 database.search()

Input In [75], in database.search()
     45 cur = conn.cursor()
     46 x = ("""SELECT * FROM v WHERE index = 1221;""")
---> 47 h = cur.execute(x).fetchall()
     48 print(h)
     49 conn.close()

OperationalError: near "index": syntax error

数据基本上是存储在存储在SQLite数据库中的 Dataframe 中的CSV。对于REMOVE函数,我只是尝试删除一行数据,而对于Search函数,我只是试图调出特定的索引。

bq8i3lrv

bq8i3lrv1#

由于“index”是here列出的保留关键字,您有没有尝试添加方括号[index]或如果可以的话重命名此字段?也许在我看来,像[v_id]这样的东西会是更好的选择。

相关问题