ODBC.Net 连接到 MySQL DB 的问题

发布于 2024-09-25 22:56:06 字数 1038 浏览 2 评论 0原文

我认为这里的问题出在我的 SQL 语法上,但我不确定,需要用新的眼光来检查。这是我用来连接然后插入数据库的代码:

OdbcConnection datConn = CreateDataConn();

datConn.Open();
OdbcCommand comm = new OdbcCommand();
comm.CommandText = "INSERT INTO userdata (key, secretkey, uid) VALUES ('" + token + "', '" + secret + "', '" + twitterid + "');";

comm.Connection = datConn;
comm.ExecuteNonQuery();
datConn.Close();

下面是 CreatDataConn() 方法:

private OdbcConnection CreateDataConn()
{
    OdbcConnection dbConn = new OdbcConnection();
    dbConn.ConnectionString = "Dsn=MySQL;database=twittertest;option=0;port=0;server=localhost;uid=root;pass=Red!4jedi";
    return dbConn;
}

我创建了一个指向数据库的 DSN,该数据库托管在我的计算机上。

当我运行该应用程序时,我收到此错误:

错误 [42000] [MySQL][ODBC 3.51 驱动程序][mysqld-5.1.51-community]您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在“key、secretkey、uid”附近使用的正确语法 VALUES(第 1 行的“127090765-i3aZl71LPSVUCPZs9kHSYeBli0vWpbq0BaM1roYC”

但对于我的一生,我无法弄清楚我的值有什么问题语法...这可能很简单,但同样,我需要一双新的眼睛来看待这个。

I'm thinking the problem here is with my SQL Syntax, but I'm not sure and need a fresh pair of eyes to check it out. This is the code I'm using to connect to and then insert into the DB:

OdbcConnection datConn = CreateDataConn();

datConn.Open();
OdbcCommand comm = new OdbcCommand();
comm.CommandText = "INSERT INTO userdata (key, secretkey, uid) VALUES ('" + token + "', '" + secret + "', '" + twitterid + "');";

comm.Connection = datConn;
comm.ExecuteNonQuery();
datConn.Close();

And here is the CreatDataConn() method:

private OdbcConnection CreateDataConn()
{
    OdbcConnection dbConn = new OdbcConnection();
    dbConn.ConnectionString = "Dsn=MySQL;database=twittertest;option=0;port=0;server=localhost;uid=root;pass=Red!4jedi";
    return dbConn;
}

I created a DSN to the database, which is hosted on my machine.

When I run the application I get this error:

ERROR [42000] [MySQL][ODBC 3.51 Driver][mysqld-5.1.51-community]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key, secretkey, uid) VALUES ('127090765-i3aZl71LPSVUCPZs9kHSYeBli0vWpbq0BaM1roYC' at line 1

But for the life of me I can't figure out what's wrong with my syntax...It's prolly something simple, but again, I need a pair of fresh eyes to look at this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转身以后 2024-10-02 22:56:06

key 可能是一个保留字。在 MySQL 中,您可以通过在列名称周围添加反引号 (`) 来解决此问题(因此使用“key”而不是“key”),但应尽量不要使用保留字作为实体名称。

key is a probably a reserved word. in MySQL, you can get around this by adding backticks (`) around a column name (so '`key`'instead of 'key'), but you should try not to have reserved words as entity names.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文