Ⅰ 有高人告一下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