Mysql batch imports text data into the database C language (c api), mysqlapi

Source: Internet
Author: User
Tags strtok

Mysql batch imports text data into the database C language (c api), mysqlapi

In fact, the principle is very simple. I think everyone should have read the article I wrote (a code learns to operate databases in C) on linux, the basic student information management system (add, delete, query, and modify to display) is implemented by operating the database in C language. Where is the insert function ), then, use the sprintf function to format and write it into the execution statement."Add"Same.



The Code is as follows:


# Include <stdio. h> # include <stdlib. h> # include <string. h> # include <mysql. h> MYSQL;/****** import data to the database in batches using the insert into command ******/void insert (char * a, char * B, char * c) {int t, r; char query [400]; memset (query, 0,400); sprintf (query, "insert into EnglishWord4 (Words, Translate, Num) values ('% s',' % s', '% s') ", a, B, c); // Insert the table printf (" executing: % s \ n ", query); t = mysql_real_query (& mysql, query, (unsigned int) strlen (query); if (t) {p Rintf ("exception occurred during insertion: % s", mysql_error (& mysql);} else {printf ("added successfully \ n") ;}} int main () {FILE * fp; char buff [300]; fp = fopen ("englishword4.txt", "r"); // open different files if (fp = NULL) {printf ("Fail to open \ n"); exit (1);} MYSQL_RES * res; MYSQL_ROW row; mysql_init (& mysql); if (! Mysql_real_connect (& mysql, "localhost", "root", NULL, "English", 0, NULL, 0) {printf ("Error connecting to database: % s ", mysql_error (& mysql);} else {printf ("Connected... \ n ") ;}char * p; char a [20]; char B [200]; char c [10]; int length = 0; while (fgets (buff, 300, fp )! = NULL) {if (buff [strlen (buff)-1] = '\ n') {buff [strlen (buff)] =' \ 0 ';} strcpy (a, strtok (buff, "#"); int I = 0; while (p = strtok (NULL ,"#"))) {if (I = 0) {// printf ("Skip \ n") ;}if (I = 1) {strcpy (B, p );} if (I = 2) {strcpy (c, p) ;}++ I ;}length = strlen (c); c [length-2] = '\ 0 '; insert (a, B, c); bzero (a, sizeof (a); bzero (B, sizeof (B); bzero (B, sizeof (c ));} mysql_close (& mysql); fclose (fp); return 0 ;}


You can try appending an operation table. This is my format.

A # [ei] # art. 1; Any; each 1 #1 able # ['ibl] # adj. yes #1 about # [comment 'baut] # prep. about #1 above # [comment 'B into v] # prep. in... above # 1act # [mongokt] # vt. action # 1add # [mongod] # vi. add; add #1 afraid # [comment 'freid] # adj. scared #1 after # ['usage: ft finished] # adv. later #1 again # [comment 'principal] # adv. again #1 against # [comment 'prop enst] # prep. objection # 1age # [eid token] # n. age # 1ago # [comment '00000000u] # adv. previous #1 agree # [comment 'invalid ri:] # vt. agree; in favor of # 1air # [ε limit] # n. air # 1all # [Clerk: l] # adj. all n. all #1 allow # [comment 'lau] # vt. #1 also # ['allowed: ls accepted u] # adv. also #1 always # ['usage: lwe Iz] # adv. Always; always #1 am # [MB m] # v. Yes #1 among # [comment'm character done] # prep. In... In the middle... # 1an # [limit n] # art. 1 (before the vowel) # 1and # [Semi nd] # conj. and #1 anger # ['commandid commandid] # n. angry; angry #1 animal # ['~nim ~l] # n. animal #1 answer # ['sequence: ns sequence] # vt. answer # 1any # ['eni] # adj. any #1 appear # [comment 'Pi done] # vi. #1 apple # ['physical pl] # n. apple; guy # 1are # [tips:] # v. yes (the second person of be in the singular and plural form now) #1 area # ['ε ə ri] # n. region # 1arm # [region: m] # n. arm #1 arrange # [comment 'reind done] # vt. schedule #1 arrive # [comment 'raiv] # vi. arrive at # 1art # [hour: t] # n. art # 1as # [z] # conj. because # 1ask # [latency: sk] # vt. question # 1at # [partition t] # prep. in #1 atom # ['doesn t need m] # n. atom #1

Simple thinking


Result:

Start table search results


Create the table and display content of the imported data


Execute the insert program:


View the contents of the inserted Table








 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.