Database connection and save data from c++ program to MySQL - c++

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. I informed on Google about databases, and I came across with MySQL, and in particular database connection. 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. 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.
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.
So here are scrypt for MySQL
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:
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.
You can use ODBC + MySQL ODBC Connector. It is better since you are not limitted in your c++ to a specific DB. If you are sure you will use only MySQL you can use also MySQL C++ Connector directly. Anyway, both will give you option to run SQL commands on your DB.
HERE you can fine MySQL c++ connector sample
HERE you can find ODBC sample.

Related

Importing to SQL Mysql instance

I'm importing data from Storage to SQL GCP (MySql datacloud). When I select the .sql file and the database there is an error message: Sorry, there’s a problem. If you entered information, check it and try again. Otherwise, the problem might clear up on its own, so check back later.
I'm trying using only one catalog table and I have the same mistake. Could you help me? please
Have you created the database before?
It seems that the problem might be with the sql file, that file is a sql dump?
The normal process for migrating to CloudSQL would be:
Create the instance, if your source will be (MySQL) you need to select MySQL.
Create database
Select the database, IMPORT
Select from sql file, and select the file
You need to have permissions for creating an instance and database.
Are you able to input correct parameters like (correct GCS path, valid file, correct database name and table)? Please note that for compressed (.gz file) and sql (dump) file, SQL format should be selected instead of CSV.

How to transfer data from one database to another in Django?

I am recreating a web app in Django that was running in a server but it was terminated, fortunately, I did a backup of all the code. My problem comes with the database because but I do not know how to transfer all the data from the old db.sqlite3 Django database web app into the new one.
I found a similar question as mine Django: transfer data from one database to another but the user wanted to transfer data from specific columns because their models.pyfrom the old and new databases were slightly different. In my case, my models.py from the old and new databases are the same.
Alternatives
I am using the DB Browser for SQLite to explore the content of the old database and I could add manually each row into the Django administration but this will take me too much time.
I could copy the old db.sqlite and replace it in the new web app because the models.py file remains the same but this solution is not appropriate IMO, this solution is rude and I think it goes against the good practices of Software.
How should I proceed for transferring data from the old database to the new one?
This seems like a one time copy of one db to another. I don't see how this goes against good software practice unless you have to be copying this db frequently. I've done it before when migrating servers and it doesn't cause any issues assuming the two instances of the application are the same build.
I was able to do some minor tricks in order to solve my problem because there is not a straightforward functionality that allows you to transfer all your data from two sqlite databases in Django. Here is the trick I did:
Download the sqlite db browser to explore and export the contents of your old database in a .csv file. Open you database with sqlite db browser and hit on the tables option and you will see all your tables, then do a right click on any of those and hit the export as a csv file option to generate the csv file (name_of_your_csv_file.csv). The other alternative is to use the sqlite3.exe to open your database in cmd or powershell and then doing the export with:
.tables #this lets you explore your available tables
headers on
mode csv
output name_of_your_csv_file.csv
2.There are two choices up to this point: You can either insert all the records at once to your new database or you can drop your existing tables from the new database and then recreate them and import the .csv file. I went for the drop option because there were more than 100 records to migrate.
# sqlite3
# check the structure of your table so you can re-create it
.schema <table_name>
#the result might be something like CREATE TABLE IF NOT EXISTS "web_app_navigator_table" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "ticket" varchar(120) NOT NULL);
#drop the table
drop table web_app_navigator_table
#re-create the table
create table web_app_navigator_table(id integer not null primary key autoincrement, ticket varchar(120) not null);
#do the import of the csv file
.import C:/Users/a/PycharmProjects/apps/navigator/name_of_your_csv_file.csv table_name_goes_here
You might see an error such as csv:1: INSERT failed datatype mismatch but this indicates that the first row of your csv file was not inserted because it contains the headers of the exported data from your old database.

Create a c++ file for using multiple database

I have the c++ project working on MySQL database.
recently i am moving to the PostgreSQL database so i am facing the difficulties like the queries in sql written is like
select UserName form UserInfo;
will not work in pgSQL it would be like
select "UserName" from "UserInfo";
so i want to make a file that could tackle this two difference syntax could anyone help me?
You should standardize on ANSI SQL, in this case "UserName" which MySQL will understand too under appropriate SQL modes. Then you should look at the minimum you need to do to support this (in this case, if it is MySQL, set the appropriate sql_mode).

How to query a DB table from C++

I have C++ code, and from it I need to access the DB and make a query in table (with name NECE_TABLE, which has 2 columns - IntID and Status).
Here I need to get "status" column value from DB table (NECE_TABLE) using the IntID from C++ code.
Any help will be greatly helpful. Thanks in advance
Your question is very vague, but in summary you need to:
Use an appropriate client library supported by your database to connect to that database using some user credentials with appropriate permissions for SELECTing from your table
Execute a SQL select to fetch the data you want
There's some confusion as to which database you're using.
If you're using Oracle, you can use the OCCI client library to connect to the database and execute SQL statements. See section 2 of the linked document, where it describes connecting to a database and executing SQL queries.
Take a look at this link - it's a simple tutorial on how to get started with MySQL and C++. You say you are using vanilla SQL in your tags, but the two should be compatible as long as you stick to the more basic queries.

How can I dump a MySQL database from mysql c library

I want to archive my database of mysql. Kindly give me some guide lines how I can make it possible, I am using mysql c library for insertion and selection etc. I dont know how to use dump command.
Use SHOW TABLES and DESCRIBE tbl_name queries to obtain structure of database and tables. Then, use SELECT to fetch data and proceed it to your output according to the structure.