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;
}
...全文
103 11 打赏 收藏 转发到动态 举报
写回复
用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
  • 打赏
  • 举报
回复
求大家指点
/** * 使用给定密码压缩指定文件或文件夹到指定位置. *

* dest可传最终压缩文件存放的绝对路径,也可以传存放目录,也可以传null或者"".
* 如果传null或者""则将压缩文件存放在当前目录,即跟源文件同目录,压缩文件名取源文件名,以.zip为后缀;
* 如果以路径分隔符(File.separator)结尾,则视为目录,压缩文件名取源文件名,以.zip为后缀,否则视为文件名. * @param src 要压缩的文件或文件夹路径 * @param dest 压缩文件存放路径 * @param isCreateDir 是否在压缩文件里创建目录,仅在压缩文件为目录时有效.
* 如果为false,将直接压缩目录下文件到压缩文件. * @param passwd 压缩使用的密码 * @return 最终的压缩文件存放的绝对路径,如果为null则说明压缩失败. */ public static String zip(String src, String dest, boolean isCreateDir, String passwd) { File srcFile = new File(src); ZipParameters parameters = new ZipParameters(); parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // 压缩方式 parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); // 压缩级别 if (!StringUtils.isEmpty(passwd)) { parameters.setEncryptFiles(true); parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); // 加密方式 parameters.setPassword(passwd.toCharArray()); } try { ZipFile zipFile = new ZipFile(dest); if (srcFile.isDirectory()) { // 如果不创建目录的话,将直接把给定目录下的文件压缩到压缩文件,即没有目录结构 if (!isCreateDir) { File [] subFiles = srcFile.listFiles(); ArrayList<File> temp = new ArrayList<File>(); Collections.addAll(temp, subFiles); zipFile.addFiles(temp, parameters); return dest; } zipFile.addFolder(srcFile, parameters); } else { zipFile.addFile(srcFile, parameters); } return dest; } catch (ZipException e) { e.printStackTrace(); } return null; } /** * 使用给定密码解压指定的ZIP压缩文件到指定目录 *

* 如果指定目录不存在,可以自动创建,不合法的路径将导致异常被抛出 * @param zipFile 指定的ZIP压缩文件 * @param dest 解压目录 * @param passwd ZIP文件的密码 * @return 解压后文件数组 为空则解压到当前目录 * @throws ZipException 压缩文件有损坏或者解压缩失败抛出 */ public static File [] unzip(File zipFile, String dest, String passwd) throws ZipException { ZipFile zFile = new ZipFile(zipFile); zFile.setFileNameCharset("GBK"); if (!zFile.isValidZipFile()) { throw new ZipException("压缩文件不合法,可能被损坏."); } if(dest ==null || dest.equals("")){ dest = zipFile.getParentFile().getAbsolutePath(); } File destDir = new File(dest); if (destDir.isDirectory() && !destDir.exists()) { destDir.mkdir(); } if (zFile.isEncrypted()) { zFile.setPassword(passwd.toCharArray()); } zFile.extractAll(dest); List<FileHeader> headerList = zFile.getFileHeaders(); List<File> extractedFileList = new ArrayList<File>(); for(FileHeader fileHeader : headerList) { if (!fileHeader.isDirectory()) { extractedFileList.add(new File(destDir,fileHeader.getFileName())); } } File [] extractedFiles = new File[extractedFileList.size()]; extractedFileList.toArray(extractedFiles); return extractedFiles; }

简单的android文件管理器源码,从书上的例子改来的。 @打开没有权限的文件夹死机 @二级目录前面有"/" @修改图标 @修改排序,先目录,后文件,不区分大小写 @单击直接打开,长按弹出选项 ├── AndroidManifest.xml ├── assets ├── bin │   ├── classes.dex │   ├── com │   ├── FileManager.apk │   └── resources.ap_ ├── default.properties ├── FileManager.launch ├── gen │   └── com │   └── ckl │   └── android │   └── FileManager │   └── R.java ├── res │   ├── drawable │   │   ├── addfolderr.png │   │   ├── audio.png │   │   ├── back.png │   │   ├── delete.png │   │   ├── flash3.png │   │   ├── flash.png │   │   ├── folder.png │   │   ├── goroot.png │   │   ├── icon.png │   │   ├── image.png │   │   ├── packed.png │   │   ├── paste.png │   │   ├── reload.png │   │   ├── text.png │   │   ├── unkown.png │   │   ├── uponelevel.png │   │   ├── video2.png │   │   ├── video.png │   │   ├── webtext2.png │   │   └── webtext.png │   ├── drawable-hdpi │   ├── drawable-ldpi │   ├── drawable-mdpi │   ├── layout │   │   ├── dialog.xml │   │   └── rename.xml │   └── values │   ├── fileendings.xml │   └── strings.xml └── src └── com └── ckl └── android └── FileManager ├── CONST.java ├── FileManager.java ├── IconifiedText.java ├── IconifiedTextListAdapter.java └── IconifiedTextView.java

1,178

社区成员

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

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