c mysql文件读取数据库文件_c语言关于从数据库读取数据写文件

展开全部

#include  

exec sql include sqlca;

int main(){

exec sql begin declare section;

char userpasswd[30]="openlab/123456";

struct{

int  id;

char name[30];

double salary;

}emp;

exec sql end   declare section;

exec sql connect:userpasswd;

exec sql declare empcursor cursor for

select id,first_name,salary  from

s_emp order by salary;

exec sql  open  empcursor;

exec sql  whenever notfound do break;

for(;;){

exec sql  fetch empcursor into :emp;

printf("%d:%s:%lf\n",emp.id,emp.name,

emp.salary);

}

exec sql  close empcursor;

exec sql commit work release;

}

把数据62616964757a686964616fe78988e69d8331333332626138存到结构体里。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用C语言中的文件操作函数从文件读取一行内容,然后将其插入到数据库中。以下是一个示例代码: ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #include <mysql/mysql.h> #define MAX_LINE_LENGTH 1024 int main() { // 建立数据库连接 MYSQL *conn = mysql_init(NULL); if(conn == NULL) { printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn)); return 1; } if(mysql_real_connect(conn, "localhost", "username", "password", "database", 0, NULL, 0) == NULL) { printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn)); return 1; } // 打开文件读取每一行数据,插入到数据库中 FILE *file = fopen("filename.txt", "r"); if(file == NULL) { printf("Error opening file.\n"); return 1; } char line[MAX_LINE_LENGTH]; while(fgets(line, MAX_LINE_LENGTH, file) != NULL) { // 去掉换行符 char *pos; if ((pos=strchr(line, '\n')) != NULL) { *pos = '\0'; } // 插入到数据库中 char *query = (char*) malloc(sizeof(char) * (strlen(line) + 128)); sprintf(query, "INSERT INTO table_name (column_name) VALUES ('%s')", line); if(mysql_query(conn, query) != 0) { printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn)); return 1; } free(query); } fclose(file); // 关闭数据库连接 mysql_close(conn); return 0; } ``` 此代码假定文件中每一行数据的最大长度为1024个字符,读取文件时每次读取一行,然后去掉换行符,最后将该行数据插入到名为`table_name`的表的`column_name`列中。您需要将`username`、`password`、`database`、`table_name`和`column_name`替换为实际的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值