我在导入SQLite3的C函数正确到C# [英] Am I Importing a SQLite3 C Function Correctly into C#

查看:219
本文介绍了我在导入SQLite3的C函数正确到C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图导入内部C#一个SQLite3的C函数,但我不确定我已经正确导入它,即,该函数参数的数据类型是正确的,该函数正确使用?该函数被用来插入图像数据(.PNG)成SQLITE3表

这是从 SQLite的网站实际的C实现的:

  INT sqlite3_bind_blob(sqlite3_stmt *,INT,常量无效*,整数N,无效(*)(无效*));

这是我在C#中导入:

 函数[DllImport(sqlite3的入口点=sqlite3_bind_blob)]
私人静态外部INT sqlite3_bind_blob(IntPtr的stmHandle,诠释iIndex,串IPARAM,诠释iBytes,诠释一个IOperation);

在C#中的SQLite3功能的使用(注意函数返回 21 - 20 SQLITE_MISMATCH / *数据类型不匹配* /

  // BLOB是一个byte []
查询字符串=插入或更换INTO myTable的(纬度,经度,图像)VALUES({1},{2},1?);
sqlite3_ prepare_v2(_connection,查询,query.Length,出stmHandle,IntPtr.Zero)
INT解析度= sqlite3_bind_blob(stmHandle,1,blob.ToString(),blob.Length,SQLITE_TRANSIENT);

问题:结果
- 我有没有用正确的参数的数据类型正确导入sqlite3_bind_blob DLL函数结果?
- 我是正确的使用功能,也就是说,我应该将BLOB转换为字符串,我是正确获取BLOB的(字节数组)长度


解决方案

 函数[DllImport(sqlite3的入口点=sqlite3_bind_blob,CallingConvention = CallingConvention.Cdecl)
私人静态外部INT sqlite3_bind_blob(IntPtr的stmHandle,INT iIndex,字节[] IPARAM,诠释iBytes,IntPtr的一个IOperation);

将是一个更好的签名。这主要是因为 System.String 是与不确定的内部结构的类的引用,它甚至不是等同于 wchar_t的**

通过校正进口,调用看起来像

 中期业绩= sqlite3_bind_blob(stmHandle,1,一滴,blob.Length,SQLITE_TRANSIENT);

而事实上,这正是SQLite的.NET包装的什么人做的。

I am attempting to import a SQLite3 C function inside C# but I am unsure I have imported it correctly, ie, that the function parameter data types are correct and that the function is being used correctly? The function is being used to insert a images data(.png) into a Sqlite3 table.

This is the actual C implementation from the SQLite website:

int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));

This is my import in C#:

[DllImport("sqlite3", EntryPoint = "sqlite3_bind_blob")]
private static extern int sqlite3_bind_blob (IntPtr stmHandle, int iIndex, string iParam, int iBytes, int iOperation);

Usage of the SQLite3 function in C# (Note the function returns 21 - SQLITE_MISMATCH 20 /* Data type mismatch */):

// blob is a byte[]
string query = "INSERT OR REPLACE INTO myTable(lat, lon, image) VALUES({1}, {2}, ?1)";
sqlite3_prepare_v2 (_connection, query, query.Length, out stmHandle, IntPtr.Zero)
int res = sqlite3_bind_blob (stmHandle, 1, blob.ToString(), blob.Length, SQLITE_TRANSIENT);

Questions:
- Have I imported the sqlite3_bind_blob DLL function correctly with the correct parameter data types?
- Am I using the function correctly, ie, should I be converting the blob to a string and am I correctly obtaining the blob's (byte array's) length?

解决方案

[DllImport("sqlite3", EntryPoint = "sqlite3_bind_blob", CallingConvention = CallingConvention.Cdecl)]
private static extern int sqlite3_bind_blob (IntPtr stmHandle, int iIndex, byte[] iParam, int iBytes, IntPtr iOperation);

would be a better signature. Mainly because System.String is a reference to a class with unspecified internal structure, it's not even equivalent to wchar_t**.

With the corrected import, the invocation will look like

int res = sqlite3_bind_blob (stmHandle, 1, blob, blob.Length, SQLITE_TRANSIENT);

and indeed, that's exactly what one of SQLite .NET wrappers do.

这篇关于我在导入SQLite3的C函数正确到C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆