Mysql batch imports text data into the database C language (CAPI) _ MySQL

Source: Internet
Author: User
Tags strtok
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) in linux, the basic student information management system (add, delete, query, and modify to display) is implemented by operating the database in C language. http: blogcsdnnetguoqianqian5812 is actually a simple principle, I think everyone should have read the article I wrote (a code learns to operate the database in C language). On linux, the basic student information management system (add, delete, query, and modify the display) is implemented through C language database operations) insert function, and then use the sprintf function to format and write it to the execution statement. "Add"Same.

The code is as follows:

[Plain] view plaincopyprint? # Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
MYSQL 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) {printf ("exception occurred during insertion: % s", mysql_error (& mysql);} else {printf ("added \ 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.

[Plain] view plaincopyprint? 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 #1 act # [mongokt] # vt. action #1 add # [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 #1 age # [eid region] # n. age #1 ago # [Comment '00000000u] # adv. previous #1 agree # [Comment 'invalid ri:] # vt. agree; in favor of #1 air # [ε limit] # n. air #1 all # [clerk: l] # adj. all n. all #1 allow # [Comment 'lau] # vt. #1 also # ['allowed: ls accepted u] # adv. also #1 always # ['Usage: lweiz] # adv. always; always #1 am # [m] # v. yes #1 among # [Comment'm character encoding] # prep. in... In the middle... #1 an # [limit n] # art. 1 (before the vowel) #1 and # [semi nd] # conj. and #1 anger # ['commandid commandid] # n. angry; angry #1 animal # ['~nim ~l] # n. animal #1 answer # ['sequence: ns sequence] # vt. answer #1 any # ['eni] # adj. any #1 appear # [Comment 'pi done] # vi. #1 apple # ['physical pl] # n. apple; Guy #1 are # [tips:] # v. yes (the second person of be in the singular and plural form now) #1 area # ['ε ə ri] # n. region #1 arm # [region: m] # n. ARM #1 arrange # [Comment 'reind done] # vt. schedule #1 arrive # [Comment 'raiv] # vi. reach #1 art # [random: t] # n. art #1 as # [symbol z] # conj. because #1 ask # [latency: sk] # vt. question #1 at # [limit 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

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.