简体   繁体   English

数据库连接并将数据从C ++程序保存到MySQL

[英]Database connection and save data from c++ program to MySQL

I never had to deal with database, therefore, sorry in advance! 我从来不需要处理数据库,因此,对不起!

I was asked to create a database for a project and store data output from a c++ program into the database. 我被要求为一个项目创建一个数据库,并将c ++程序的数​​据输出存储到数据库中。 I informed on Google about databases, and I came across with MySQL, and in particular database connection. 我在Google上了解了有关数据库的信息,并且遇到了MySQL,尤其是数据库连接。 As far as I understood, in the first place a database has to be created (for example with MySQL), and once data are inserted, it's possible to access to them. 据我了解,首先必须创建一个数据库(例如,使用MySQL),一旦插入数据,便可以访问它们。 However, it's not totally clear what is possible to achieve with such a connection and how to save data from a c++ program into the database directly. 但是,还不清楚用这种连接可以实现什么以及如何将数据从c ++程序直接保存到数据库中。

Based on what I read on the net, these should be related, is it right? 根据我在网上阅读的内容,这些应该相关,对吗? I would really need some help, example or clarification about these two questions. 对于这两个问题,我真的需要一些帮助,示例或说明。 Thanks in advance for your time! 在此先感谢您的时间!

First you should create DB and tables. 首先,您应该创建数据库和表。 You can do it in each DB IDE wizards, or you can write it in a script. 您可以在每个DB IDE向导中执行此操作,也可以在脚本中编写它。

So here are scrypt for MySQL 所以这里是MySQL的scrypt

CREATE DATABASE test_db --this create DB called test_db

I guess you should store a message and a timestamp so a possible table (In MySQL) will be: 我想您应该存储一条消息和一个时间戳,以便可能的表(在MySQL中)为:

USE test_db -- from now on the script using test_db unless specified explicit DB
--creating table with id, mmessage and timestamp 
CREATE TABLE output_table ( 
   msg_ID INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
   msg VARCHAR(max),
   msg_TS TIMESTAMP DEFAULT CURRENT_TIMESTAMP)   

In the above table you only need to give the message since all the rest are filled automatically. 在上面的表中,您只需要给出消息,因为其余所有信息都会自动填充。 So Insert into the table command may look like this: 因此,插入表命令可能如下所示:

INSERT INTO output_table (msg) values ('this is a message')

When you want to check the whole table you run the following: 当您要检查整个表时,请运行以下命令:

SELECT * FROM output_table 

Now you need to connect this code to your c++ code: Generally, you'll need to know db name, user name and password in manner to connect to DB. 现在,您需要将此代码连接到c ++代码:通常,您需要以连接数据库的方式知道数据库名称,用户名和密码。

You can use ODBC + MySQL ODBC Connector. 您可以使用ODBC + MySQL ODBC Connector。 It is better since you are not limitted in your c++ to a specific DB. 这样做会更好,因为您不会将c ++限于特定的DB。 If you are sure you will use only MySQL you can use also MySQL C++ Connector directly. 如果确定只使用MySQL,则可以直接使用MySQL C ++ Connector Anyway, both will give you option to run SQL commands on your DB. 无论如何,两者都会为您提供在数据库上运行SQL命令的选项。

  • HERE you can fine MySQL c++ connector sample 在这里您可以完善MySQL c ++连接器示例
  • HERE you can find ODBC sample. 在这里您可以找到ODBC示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM