当前位置:首页 » 编程语言 » gcc操作sql
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

gcc操作sql

发布时间: 2023-03-16 08:07:23

Ⅰ 有高人告一下c语实际操作sql server的select *from mytables 我IP端口库名分别是127.0.0.1:1433 mydb;

1、端口的写法错误,应为:127.0.0.1,1433

2、C/C++可以使用ODBC连接MSSQL数纤租据库,大致如下:

1)创建ODBC数据源

2)C/C++程序链接数据库

#include<stdio.h>
#include<string.h>
#include<windows.h>
#include<sql.h>
#include<sqlext.h>
#include<sqltypes.h>
#include<odbcss.h>
#defineMAXBUFLEN255
SQLHENVhenv=SQL_NULL_HENV;
SQLHDBChdbc1=SQL_NULL_HDBC;
SQLHSTMThstmt1=SQL_NULL_HSTMT;

/*cpp文件功能说明:
1.数据库操作中的添加,修改,删除,主要体现在SQL语句上a
2.采用直接执行方式和参数预编译执行方式两种
*/

intmain()
{
RETCODEretcode;

UCHARszDSN[15]="mydb";//数据源名称
UCHARuserID[6]="sa";//数据库用户ID
UCHARpassWORD[29]="123456";//用户密码

//1.连接数据源
//1.环境句柄
retcode=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&henv);
retcode=SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,(SQLPOINTER)SQL_OV_ODBC3,SQL_IS_INTEGER);
//2.连接句柄
retcode=SQLAllocHandle(SQL_HANDLE_DBC,henv,&hdbc1);
retcode=SQLConnect(hdbc1,szDSN,SQL_NTS,userID,SQL_NTS,passWORD,SQL_NTS);
//判断连接是否成功
if((retcode!=SQL_SUCCESS)&&(retcode!=SQL_SUCCESS_WITH_INFO))
{
printf("连接失败! ");
}
else
{
/*
1.分配一个语句句柄(statementhandle)
2.创建SQL语句
3.执行语句
4.销毁语毁键兆句
*/
retcode=SQLAllocHandle(SQL_HANDLE_STMT,hdbc1,&hstmt1);
char亮并sql[100]="insertintotestvalues(22,22)";

Ⅱ 如何在Linux下用C语言操作数据库sqlite3

下面我们看看怎么在C语言中向数据库插入数据。
好的,我们现编辑一段c代码,取名为 insert.c
// name: insert.c
// This prog is used to test C/C++ API for sqlite3 .It is very simple,ha !
// Author : zieckey All rights reserved.
// data : 2006/11/18
#include <stdio.h>
#include <stdlib.h>
#include "sqlite3.h"
#define _DEBUG_
int main( void )
{
sqlite3 *db=NULL;
char *zErrMsg = 0;
int rc;
rc = sqlite3_open("zieckey.db", &db); //打开指定的数据库文件,如果不存在将创建一个同名的数据库文件
if( rc )
{
fprintf(stderr, "Can't open database: %s
", sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
}
else printf("You have opened a sqlite3 database named zieckey.db successfully!
Congratulations! Have fun ! ^-^
");
//创建一个表,如果该表存在,则不创建,并给出提示信息,存储在 zErrMsg 中
char *sql = " CREATE TABLE SensorData(
ID INTEGER PRIMARY KEY,
SensorID INTEGER,
SiteNum INTEGER,
Time VARCHAR(12),
SensorParameter REAL
);" ;
sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
#ifdef _DEBUG_
printf("%s
",zErrMsg);
#endif
//插入数据
sql = "INSERT INTO "SensorData" VALUES( NULL , 1 , 1 , '200605011206', 18.9 );" ;
sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
sql = "INSERT INTO "SensorData" VALUES( NULL , 1 , 1 , '200605011306', 16.4 );" ;
sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
sqlite3_close(db); //关闭数据库
return 0;
}

好的,将上述代码写入一个文件,并将其命名为 insert.c 。
解释:
sqlite3_exec的函数原型说明如下:
int sqlite3_exec(
sqlite3*,
const char *sql,
sqlite_callback,
void *,
char **errms

g
);

编译:
[root@localhost temp]# gcc insert.c -lsqlite3 -L/usr/local/sqlite3/lib -I/usr/local/sqlite3/include
insert.c:28:21: warning: multi-line string literals are deprecated
[root@localhost temp]#
执行
[root@localhost temp]# ./a.out
./a.out: error while loading shared libraries: libsqlite3.so.0: cannot open shared object file: No such file or directory
[root@localhost temp]#
同样的情况,如上文处理方法:
[root@localhost temp]# export LD_LIBRARY_PATH=/usr/local/sqlite3/lib:$LD_LIBRARY_PATH
[root@localhost temp]# ./a.out
You have opened a sqlite3 database named zieckey.db successfully!
Congratulations! Have fun ! ^-^
(null)
(null)
(null)
[root@localhost temp]#
运行成功了,好了,现在我们来看看是否插入了数据
[root@localhost temp]# /usr/local/sqlite3/bin/sqlite3 zieckey.db
SQLite version 3.3.8
Enter ".help" for instructions
sqlite> select * from SensorData;
1|1|1|200605011206|18.9

2|1|1|200605011306|16.4
sqlite>

Ⅲ 对postgresql数据库进行操作,把sql文件导入,出现以下错误,是怎么回事怎么解决有没有大神帮帮忙

function postorder($root)
configure:3458: checking whether the C compiler works
configure:3480: gcc conftest.c >&5
ld: malformed 32-bit x.y.z version number: 11.4.0d1
collect2: ld returned 1 exit status
configure:3484: $? = 1
configure:3522: result: no
configure: failed program was:
| /* confdefs.h */

Ⅳ linux gcc mysql 如何在C语言中使用嵌入式SQL编程

我,我发给你,全面覆盖了gcc编程,当然是简单版本,不过基本够用了

Ⅳ C语言db2嵌入式SQL编程,编译问题 undefined reference to `sqlastrt'

1、要有类似的定义:
……
EXEC SQL INCLUDE SQLDA; /* or #include <sqlda.h> */
2、编译环境要有db2的权限和sqllib的路径
3、我已经上传了一份相关的文档,预计明后天审核通过就可以看到了
《DB2开发基础》
http://passport..com/?business&aid=6&un=chinacmouse#7

补充一个程序:
#include <time.h>
#include "stdio.h"

EXEC SQL INCLUDE SQLCA;

int main()
{
int i=0;
struct tm *pt;
time_t t1;
t1 = time(NULL);
pt = localtime(&t1);
printf("%4d%02d%02d", pt->tm_year+1900, pt->tm_mon+1, pt->tm_mday);
printf("%02d:%02d:%02d\n",pt->tm_hour,pt->tm_min,pt->tm_sec);

EXEC SQL CONNECT TO db;
i=0;
while (i<3000)
{
int j=0;
while (j<1000)
{
EXEC SQL update cc.fund set cc_code='095' where cc_no='0950031359';
j++;
}
i++;
}
EXEC SQL COMMIT;
t1 = time(NULL);
pt = localtime(&t1);
printf("%4d%02d%02d", pt->tm_year+1900, pt->tm_mon+1, pt->tm_mday);
printf("%02d:%02d:%02d\n",pt->tm_hour,pt->tm_min,pt->tm_sec);
EXEC SQL CONNECT RESET;
return 1;
}

编译脚本
db2 prep testdb.sqc target cplusplus bindfile using testdb.bnd package using testdb
db2 bind testdb.bnd
db2 grant execute on package testdb to public
gcc -I/app/db2inst1/sqllib/include -I./ -c -g testdb.C
gcc -L/app/db2inst1/sqllib/lib -ldb2 -L/usr/lib -lm -o testdb testdb.o

Ⅵ linux gcc mysql 如何在C语言中使用嵌入式SQL编程 要什么头文件如何编译越详细越好

最起码包含mysql.h
实例代码:
#include <stdlib.h>
#include <stdio.h>

#include "mysql.h"

int main(int argc, char *argv[]) {
MYSQL *conn_ptr;

conn_ptr = mysql_init(NULL);
if (!conn_ptr) {
fprintf(stderr, "mysql_init failed\n");
return EXIT_FAILURE;
}

conn_ptr = mysql_real_connect(conn_ptr, "localhost", "rick", "secret",
"foo", 0, NULL, 0);//连接数据库

if (conn_ptr) {
printf("Connection success\n");
} else {
printf("Connection failed\n");
}

mysql_close(conn_ptr);

return EXIT_SUCCESS;
}
编译:(假定上面文件取名 con.c,在当前目录下)
gcc -I/usr/include/mysql con.c -L/usr/lib/mysql -lmysqlclient -o con