如何将数据库的数据读取到数组中

如何将数据库的数据读取到数组中,第1张

#ifdef WIN32

#include <windowsh>

#include <odbcinsth>

#include <sqlexth>

#else

#include <mysqlh>

#include <unistdh>

#define SQLHANDLE static MYSQL

#endif

#include <stdargh>

#include <stdlibh>

#include <stdioh>

#include <stringh>

SQLHANDLE hDBEnv, hDBC;

int DB_Open(char dbcn, char usr, char pwd)

{

int r;

#ifdef WIN32

r = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hDBEnv);

if(r) return 0;

r = SQLSetEnvAttr(hDBEnv,SQL_ATTR_ODBC_VERSION,

(void)SQL_OV_ODBC3,0);

if(r) return 0;

r = SQLAllocHandle(SQL_HANDLE_DBC, hDBEnv, &hDBC);

if(r) return 0;

r = SQLConnect(hDBC,

(unsigned char )dbcn, strlen(dbcn),

(unsigned char )usr, strlen(usr),

(unsigned char )pwd, strlen(pwd));

return r==SQL_SUCCESS || r==SQL_SUCCESS_WITH_INFO;

#else

mysql_init(&hDBC);

MYSQL rx = mysql_real_connect(

&hDBC, dbcn, usr, pwd, NULL, 0, NULL, 0);

if(!rx) return 0;

return 1;

#endif

}//end DB_Open

int DB_Exec(char sql)

{

#ifdef WIN32

SQLHANDLE hStatement = NULL;

SQLAllocHandle(SQL_HANDLE_STMT, hDBC, &hStatement);

SQLExecDirect(hStatement,(unsigned char )sql, strlen(sql));

SQLCloseCursor(hStatement);

SQLFreeHandle(SQL_HANDLE_STMT, hStatement);

hStatement = NULL;

#else

mysql_real_query (&hDBC, sql, strlen(sql));

#endif

return 1;

}//end DB_Exec

int DB_Close(void)

{

#ifdef WIN32

SQLDisconnect(hDBC);

SQLFreeHandle(SQL_HANDLE_DBC, hDBC);

SQLFreeHandle(SQL_HANDLE_ENV, hDBEnv);

#else

mysql_close(&hDBC);

#endif

return 1;

}//DB_Close()

#ifndef WIN32

typedef struct tagMySQLRecordset {

MYSQL_RES hRecord;

void row;

int size;

int cols;

} MYSQLRecordset;

#endif

void DB_CleanQuery(void hRecordset)

{

if(!hRecordset) return;

#ifdef WIN32

__try {

SQLCloseCursor(hRecordset);

SQLFreeHandle(SQL_HANDLE_STMT, hRecordset);

hRecordset = NULL;

}__finally{

return;

}//end try

#else

MYSQLRecordset hRec = (MYSQLRecordset ) hRecordset;

mysql_free_result (hRec->hRecord);

free(hRec->row); hRec->row = NULL;

free(hRec->size); hRec->size = NULL;

free(hRec); hRec = NULL;

#endif

}//end DB_CleanQuery

int DB_Next(void hRecordset)

{

int r=0; if(!hRecordset) return 0;

#ifdef WIN32

r = SQLFetch(hRecordset);

r = r == SQL_SUCCESS || r == SQL_SUCCESS_WITH_INFO;

if(!r) {

DB_CleanQuery(hRecordset);

}//end if

#else

MYSQLRecordset hRec = (MYSQLRecordset ) hRecordset;

MYSQL_ROW row = mysql_fetch_row (hRec->hRecord);

if(row) {

for(int i=0; i<hRec->cols; i++) {

memcpy(hRec->row[i], row[i], hRec->size[i]);

}//next i

r = 1;

}else{

DB_CleanQuery(hRecordset);

r = 0;

}//end if

#endif

return r;

}//end DB_Next

int DB_params_count(const char fmt)

{

int i=0, j=0;

while(fmt[i]) {

if(fmt[i]=='%') j++;

i++;

}//end while

return j;

}//end DB_params_count

void DB_Query(char sql, const char fmt, )

{

int r=0;

int cols = DB_params_count(fmt);

if(cols<1) return NULL;

#ifdef WIN32

SQLHANDLE hStatement = NULL;

SQLAllocHandle(SQL_HANDLE_STMT, hDBC, &hStatement);

r = SQLExecDirect(hStatement,(unsigned char )sql, strlen(sql));

r = r==SQL_SUCCESS || r == SQL_SUCCESS_WITH_INFO;

if(!r) {

return NULL;

}//end if

#else

r = mysql_real_query (&hDBC, sql, strlen(sql));

if(r) return NULL;

MYSQL_RES rec = NULL;

rec = mysql_store_result (&hDBC);

if(!rec) return NULL;

MYSQLRecordset hStatement

= (MYSQLRecordset )malloc(sizeof(MYSQLRecordset));

if(!hStatement) return NULL;

memset(hStatement, 0, sizeof(MYSQLRecordset));

hStatement->hRecord = rec;

hStatement->cols = cols;

hStatement->row = (void )malloc(cols sizeof(void ));

memset(hStatement->row, 0, cols sizeof(void ));

hStatement->size = (int )malloc(cols sizeof(int));

memset(hStatement->size, 0, cols sizeof(int));

#endif

va_list ap; va_start(ap, fmt);

void var; char buf[32];

int i=0,j=0,k=0,sz=0; char c=0;

int len = strlen(fmt); int bad=1;

#ifdef WIN32

int col=1;

#else

int col=0;

#endif

while(fmt[i]) {

c = fmt[i++];

if(c != '%') continue;

c = fmt[i++];

var = va_arg(ap, void );

if(c == 'd') {

#ifdef WIN32

SQLBindCol(hStatement, col, SQL_C_SLONG, var, 4,NULL);

#else

hStatement->row[col] = var;

hStatement->size[col] = sizeof(long);

#endif

col++;

continue;

}//end if

if(c == 'f') {

#ifdef WIN32

SQLBindCol(hStatement, col, SQL_C_FLOAT, var, 4,NULL);

#else

hStatement->row[col] = var;

hStatement->size[col] = sizeof(float);

#endif

col++;

continue;

}//end if

memset(buf, 0, 32); bad=1;

for(j=i-1; j<len; j++) {

c = fmt[j];

if(c>='0' && c<='9') buf[j-i+1]=c;

if(c=='s') {bad=0; k=j+1; break;}

}//next j

if(bad) return NULL;

sscanf(buf, "%d", &sz);

#ifdef WIN32

SQLBindCol(hStatement, col, SQL_C_CHAR, var, sz, NULL);

#else

hStatement->row[col] = var;

hStatement->size[col] = sz;

#endif

col++;

i = k;

}//end while

va_end(ap);

return hStatement;

}//end DB_Rec

在2X的版本中实现过,代码ExtAjaxrequest({url : '',method : 'POST',params : param,timeout : 60000,success : function(form, action) {var result = ExtutilJSONdecode(formresponseText);var gridPanelStore = gridgetStore();gridPanelStoreproxy = new ExtdataPagingMemoryProxy(result);gridPanelStoreload({params : {start : 0,limit : 10 },failure : function(form, action) {}})gridPanelStoreproxy = new ExtdataPagingMemoryProxy(result);在2X中主要代码是这里,通过ExtdataPagingMemoryProxy分页一次性读取数据,这样翻页就不要重新调用数据库了。但在4X版本中找不到这个东西,急求解决方案!

{header:'XXX',dataIndex:'XXX',width:XX,renderer: function (value){

//XXX根据自己情况填写

if ( value == 1)

{

return "<img src=地址/>"; //表示ok的

} else {

return "<img src=地址/>"; //表示no的

}

}}

应该是直接对combox对象执行getValue方法和getRawValue方法,这两个函数一个获取displayField,一个获取valueField

好像不容易通过combobox获得的record对象,再get

以上就是关于如何将数据库的数据读取到数组中全部的内容,包括:如何将数据库的数据读取到数组中、ext4如何实现Ext.Ajax.request请求中一次性获取数据再加载grid、怎样用ext的grid将数据库查出来的int类型数据转换显示成图片等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9764318.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-01
下一篇 2023-05-01

发表评论

登录后才能评论

评论列表(0条)

保存