int ImportData(char *dbfile, char *zTable, char *zFile, char *separator,
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;
}