python - 将 python(语音识别文本)插入 SQL Server 数据库
问题描述
我使用带有 python 的语音识别应用程序,其中您所说的文本保存在名为“yourtext.txt”的文件中,我尝试将其连接到 SQL Server 数据库,结果也将添加到文本文件中如在 SQL Server 表中,voice2text.dbo.yourtext (id, 'text1')
任何帮助将不胜感激。
注意:我在第 19 行的 SQL 命令中有语法错误,但我不知道如何修复它...
import speech_recognition as sr
import pyaudio
import pyodbc
r = sr.Recognizer()
with sr.Microphone() as source :
print("Say Something, pwease :")
audio = r.listen(source)
try :
text = r.recognize_google(audio, language="fr-FR")
conn = pyodbc.connect('Driver={SQL Server};'
'Server=VEGA\SQLEXPRESS;'
'Database=voice2text;'
'Trusted_Connection=yes;')
cursor = conn.cursor()
sqlcommand = (" insert into yourtext values ('"text"')")
cursor.execute(sqlcommand)
conn.commit()
conn.close()
print(text,file = open("yourtext.txt","a"))
except :
print("Sorry, could not hear !")
解决方案
要从变量中加入/连接字符串,您必须使用+
sqlcommand = " insert into yourtext values ('" + text + "')"
但?
在执行中使用和发送文本作为参数会更安全
execute("INSERT INTO yourtext VALUES (?)", (text,) )
顺便说一句:execute()
需要tuple
有参数 - 即使你只有一个参数。它需要逗号(text,)
来创建tuple
。括号()
不会创建tuple
. 它们仅与text,
该行中的其他逗号分隔。
推荐阅读
- c# - “不允许从 MonoBehaviour 构造函数调用加载”在不从 MonoBehaviour 继承的脚本上
- c# - 在 VS2015/C# 中使用存储过程创建 ADO.NET 只读 SQL Server 数据集
- php - 无法从数据库中获取单个字段
- css - 如何设置滚动的大小来扩大屏幕的大小?
- html - Listitem 真正居中,其他项目围绕它流动
- mysql - SQL - 不存在与加入
- python - 从 TTK 组合框列表中删除项目
- powershell - 尝试由没有管理员权限且管理员凭据在脚本中的用户运行具有管理员权限的 PowerShell 脚本
- c++ - C++ Builder 10.3.3 [ilink32 错误] 致命:无法打开 .\Win32\Debug\TabbedApplication.exe(程序仍在运行?)
- asp.net-mvc - 从 ASP.Net MVC Web 应用程序调用 ASP.Net Web api。MVC Web 客户端未正确构建 URL,因此找不到