为什么会出现错误:Sqlite3.操作错误:users表中没有名为name的列?

问题描述 投票:0回答:1

我是一名新手程序员,我决定在telebot库上制作一个机器人,但出现错误,表中没有名称列。我将感谢您的帮助。

@bot.callback_query_handler(func=lambda callback:callback.data)
def check_callback(callback):
    if callback.data == 'moryak':
        conn = sqlite3.connect('users.sql')
        cur = conn.cursor()
        cur.execute("""CREATE TABLE users (
        id INTEGER autoincrement PRIMARY KEY,
        name TEXT, 
        title TEXT, 
        salary INTEGER,
        exper INTEGER, 
        age INTEGER,
        sea TEXT,
        dwt TEXT, 
        motor TEXT, 
        eng TEXT,
        tele TEXT,
        email TEXT,
        trip TEXT, 
        seek TEXT, 
        extra TEXT
        )""")
        conn.commit()
        cur.close()
        conn.close()

    
         @bot.message_handler(content_types=['text'])
         def finish(message):
             extra = message.text.strip()

             conn = sqlite3.connect('users.sql')
             cur = conn.cursor()
             cur.execute(f'INSERT INTO users (name, title, salary, exper, age, sea, dwt, motor, eng, tele, email, trip, seek, extra) VALUES ("{name}", "{title}", "{salary}", "{exper}", "{age}", "{sea}", "{dwt}", "{motor}", "{eng}", "{tele}", "{email}", "{trip}", "{seek}", "{extra}")')
             conn.commit()
             cur.close()
             conn.close()

我尝试使用“DROP TABLE”命令,但它不起作用。我的代码创建了一个带有only1“id”列的表。

sqlite
1个回答
0
投票

建议您需要包含您正在使用的语言是的,我们很擅长猜测这是我创建 SQLite 数据库和许多表的方法

声明一些顶级变量

Public Class frmNewData
Public connStr As String = "Data Source={0};Version=3;"
Public conn As SQLiteConnection

现在当表单加载时

Private Sub frmNewData_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    connStr = String.Format(connStr, gv_dbName)

我使用 Module 来声明全局变量,即 gv_dbName 您可能想阅读有关模块和全局变量的内容 我有一个调用下面 MakeDB 代码的按钮

   Private Sub btnMakeDB_Click(sender As Object, e As EventArgs) Handles btnMakeDB.Click
    makeDB()
    btnMakeDB.Visible = False
    btnSave.Visible = True
    btnGet.Visible = True
    tbBeginBal.ReadOnly = False
    tbDepositAmt.ReadOnly = False
    tbDate.ReadOnly = False
End Sub

现在让我们看看 makeDB 做了什么 它创建数据库和许多表

    Public Sub makeDB()

    If Not My.Computer.FileSystem.FileExists(gv_dbName) Then
        Try
            conn = New SQLiteConnection($"Data Source = '{gv_dbName}';Version=3;")
            conn.Open()
            makeDateInfo()
            makeSSData()
            makeCBBalance()
            makeTxData()

        Catch ex As Exception
            tbMessage.Text = "DB and Tables NOT Created"
        End Try
    End If

End Sub

好的,这是其中一张表的创建方式

    Public Sub makeSSData()

    Dim create_table As String = String.Empty
    create_table = "CREATE TABLE IF NOT EXISTS SSDataTable(
                    SID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
                    ssAmount TEXT,
                    ssType TEXT,
                    ssDesc TEXT)"

    Dim dbTable As String = "SSDataTable"

    If Not My.Computer.FileSystem.FileExists(dbTable) Then
        Try
            Using conn As New SQLiteConnection(connStr)
                conn.Open()
                Using cmd As New SQLiteCommand(create_table, conn)
                    cmd.ExecuteNonQuery()
                End Using
            End Using
        Catch ex As Exception
            tbMessage.Text = "SSData Table FAILED"
        End Try
    End If
End Sub

所有这些代码都在一种名为 frmNewData.vb 的形式中 当 frmStart.vb 加载时,它会调用 frmNewData.vb 来创建 具有此代码的必要数据库和表

  Private Sub frmStart_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    If My.Computer.FileSystem.FileExists(gv_dbName) Then
        btnInit.Visible = False
        Me.Height = 525
        Me.Width = 900
        Me.Refresh()
    Else
        Me.Height = 200
        Me.Width = 480

        Me.Refresh()
        btnInit.Visible = True
        btnEnterCheck.Visible = False
        btnDeposit.Visible = False
    End If

End Sub

创建数据库并创建表后 btnInit 不再出现

© www.soinside.com 2019 - 2024. All rights reserved.