int ImportData(char *dbfile, char *zTable, char *zFile, char *separator,

zhxfzls 2012-08-28 04:35:56
int ImportData(char *dbfile, char *zTable, char *zFile, char *separator, char *errmsg)

在网上查到这个函数,调用的时候不知道里面该输些什么?我的数据库名为test.db,表名为test,要导入的文件名为test.txt,要改的分隔符为“,” 后面那个就更不知道了 代码如下:(目的:将txt文件读入数据库)
static char *local_getline(char *zPrompt, FILE *in){
char *zLine;
int nLine;
int n;
int eol;

if( zPrompt && *zPrompt ){
printf("%s",zPrompt);
fflush(stdout);
}
nLine = 100;
zLine = (char *)malloc( nLine );
if( zLine==0 ) return 0;
n = 0;
eol = 0;
while( !eol ){
if( n+100>nLine ){
nLine = nLine*2 + 100;
zLine = (char *)realloc(zLine, nLine);
if( zLine==0 ) return 0;
}
if( fgets(&zLine[n], nLine - n, in)==0 ){
if( n==0 ){
free(zLine);
return 0;
}
zLine[n] = 0;
eol = 1;
break;
}
while( zLine[n] ){ n++; }
if( n>0 && zLine[n-1]=='\n' ){
n--;
zLine[n] = 0;
eol = 1;
}
}
zLine = (char *)realloc( zLine, n+1 );
return zLine;
}
int ImportData(char *dbfile, char *zTable, char *zFile, char *separator, char *errmsg)
{
sqlite3 *db;
//char dbfile[128];
//char *zTable = "Train"; /* Insert data into this table */
//char *zFile = "Station"; /* The file from which to extract data */
sqlite3_stmt *pStmt; /* A statement */
int rc; /* Result code */
int nCol; /* Number of columns in the table */
int nByte; /* Number of bytes in an SQL string */
int i, j; /* Loop counters */
int nSep; /* Number of bytes in p->separator[] */
char *zSql; /* An SQL statement */
char *zLine; /* A single line of input from the file */
char **azCol; /* zLine[] broken up into columns */
char *zCommit; /* How to commit changes */
FILE *in; /* The input file */
int lineno = 0; /* Line number of input file */
int nRet=0;

nRet=sqlite3_open(dbfile, &db);
if(nRet!=0)
{
//TRACE("%s\n",sqlite3_errmsg(&db));
sqlite3_close(db);
//pListdb=NULL;
return -1;
}
//sqlite3_key(db,m_key,strlen(m_key));

nSep = strlen(separator);
if( nSep==0 ){
sprintf(errmsg, "non-null separator required for import\n");
return -2;
}

zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable);
if( zSql==0 ) return 0;
nByte = strlen(zSql);
rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
sqlite3_free(zSql);
if( rc ){
sprintf(errmsg,"Error: %s\n", sqlite3_errmsg(db));
nCol = 0;
rc = 1;
}else{
nCol = sqlite3_column_count(pStmt);
}
sqlite3_finalize(pStmt);
if( nCol==0 ) return 0;
zSql = (char *)malloc( nByte + 20 + nCol*2 );
if( zSql==0 ) return 0;
//sqlite3_snprintf(nByte+20, zSql, "INSERT INTO '%q' VALUES(?", zTable);
sqlite3_snprintf(nByte+20, zSql, "REPLACE INTO '%q' VALUES(?", zTable); //?????????????,?INSERT???REPLACE
j = strlen(zSql);
for(i=1; i<nCol; i++){
zSql[j++] = ',';
zSql[j++] = '?';
}
zSql[j++] = ')';
zSql[j] = 0;
rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
free(zSql);
if( rc ){
fprintf(stderr, "Error: %s\n", sqlite3_errmsg(db));
sqlite3_finalize(pStmt);
return 1;
}
in = fopen(zFile, "rb");
if( in==0 ){
fprintf(stderr, "cannot open file: %s\n", zFile);
sqlite3_finalize(pStmt);
return 0;
}
azCol = (char **)malloc( sizeof(azCol[0])*(nCol+1) );
if( azCol==0 ){
fclose(in);
return 0;
}
sqlite3_exec(db, "BEGIN", 0, 0, 0);
zCommit = "COMMIT";
while( (zLine = local_getline(0, in))!=0 ){
char *z;
i = 0;
lineno++;
azCol[0] = zLine;
for(i=0, z=zLine; *z && *z!='\n' && *z!='\r'; z++){
if( *z==separator[0] && strncmp(z, separator, nSep)==0 ){
*z = 0;
i++;
if( i<nCol ){
azCol[i] = &z[nSep];
z += nSep-1;
}
}
}
*z = 0;
if( i+1!=nCol ){
sprintf(errmsg,"%s line %d: expected %d columns of data but found %d\n",
zFile, lineno, nCol, i+1);
zCommit = "ROLLBACK";
break;
}
for(i=0; i<nCol; i++){
sqlite3_bind_text(pStmt, i+1, azCol[i], -1, SQLITE_STATIC);
}
sqlite3_step(pStmt);
rc = sqlite3_reset(pStmt);
free(zLine);
if( rc!=SQLITE_OK ){
sprintf(errmsg,"Error: %s\n", sqlite3_errmsg(db));
zCommit = "ROLLBACK";
rc = 1;
break;
}
}
free(azCol);
fclose(in);
sqlite3_finalize(pStmt);
sqlite3_exec(db, zCommit, 0, 0, 0);

return 1;
}
...全文
107 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhxfzls 2012-08-28
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]
说实话,这个我帮不了你了,你还是好好看吧
[/Quote]
恩 谢谢
缘中人 2012-08-28
  • 打赏
  • 举报
回复
说实话,这个我帮不了你了,你还是好好看吧
zhxfzls 2012-08-28
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]
貌似头文件的问题了
头文件重复引用或顺序,
http://blog.csdn.net/melong100/article/details/6426161
[/Quote]
我还是没有找到错误啊
缘中人 2012-08-28
  • 打赏
  • 举报
回复
貌似头文件的问题了
头文件重复引用或顺序,
http://blog.csdn.net/melong100/article/details/6426161
zhxfzls 2012-08-28
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

C/C++ code

ImportData("test.db","test","test.txt",",", errmsg);
[/Quote]
我这样,该处就出现很多错误啊,是不是要添加什么头文件?错误如下:

error:expected declaration specifiers or ‘...’ before string constant
error: expected declaration specifiers or ‘...’ before string constant
error: expected declaration specifiers or ‘...’ before string constant
error: expected declaration specifiers or ‘...’ before string constant
error: expected declaration specifiers or ‘...’ before ‘errmsg’
缘中人 2012-08-28
  • 打赏
  • 举报
回复

ImportData("test.db","test","test.txt",",", errmsg);

zhxfzls 2012-08-28
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

人家有调用例子啊
调用方式
ImportData(dbfile, zTable, zFile, separator, errmsg);
[/Quote]
那我就该写ImportData(test.db, test, test.txt, ",", errmsg);?
缘中人 2012-08-28
  • 打赏
  • 举报
回复
人家有调用例子啊
调用方式
ImportData(dbfile, zTable, zFile, separator, errmsg);
缘中人 2012-08-28
  • 打赏
  • 举报
回复
char errmsg[200];
是错误信息
zhxfzls 2012-08-28
  • 打赏
  • 举报
回复
求大家指点
zhxfzls 2012-08-28
  • 打赏
  • 举报
回复
求大家指点

1,178

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 数据库及相关技术
社区管理员
  • 数据库及相关技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧