C++ Builder10.2 for Android操作sqlite数据库的简单实现
C++ Builder10.2 for Android操作sqlite数据库的简单实现
系统联机帮助中有开发IOS和Android应用通过TFDQuery和SQL语句操作sqlite的详细步骤,本文则是利用TFDTable控件实现数据库操作的另一种基本类似的方法。
1. 在官网上下载安装sqlite3软件,创建一个数据库test.db,在库中建立几个数据表,如mytable1等。注意,浮点数选择Double类型,不要选择numeric类型,后者是BCD类型,在网格控件上显示为(BCD),以前版本能正确显示数字。
2. 与系统联机帮助一致,在C++ builder上新建一个空白Android应用,将第一步建立的库test.db拷入该应用所在目录下。
3. 增加控件: FDPhysSQLiteDriverLink1、FDGUIxWaitCursor1、FDConnection1、FDTable1、Grid1。
4. 连接SQLite数据库:
双击FDConnection1->Definition->Driver ID=SQLite;
下方的Database设置一下数据库test.db的路径;
FDTable1->tablename-=mytable1
5. 绑定数据:
①菜单->View->LiveBindings Designer->点击绑定向导(就是最左下方的按钮)。
②LiveBindings Wizard->LInk a grid with a data source->Next。
③LiveBindings Wizard->Grid1->Next。
④LiveBindings Wizard->FDTable1->Next->Finish。
6. 发布:
菜单Project->Deployment->Add File->选择文件test.db;
Remote Path->assets\internal\。
7. 程序中增加事件处理代码:
FDConnection1BeforeConnect事件增加代码:
FDConnection1.Params.Values['Database']:=TPath.Combine(TPath.GetDocumentsPath,'test.db');
注意,中文存储乱码问题,上述建立的数据库test.db是UTF8编码格式,理论上存入的数据要用UTF8Encode()转换为该编码,直接存入中文是乱码。如果数据来自GB2312时,目前我没有找到有效的转换方法,但发现强制存入GB2312也可以,示例代码如下:
typedef AnsiString_T<936> GB2312String;
GB2312String myGstr = myPtr1[0]; //myPtr1[0]为来自网络中的GB2312编码中文字符串
FDTable1Name->AsString = myGstr;
效果如下图所示:
示例中没有用系统自带的数据库导航栏,自定义按钮进行导航也很简单,如增加新记录:FDTable1->Append();
提交修改:FDTable1->CheckBrowseMode(),等等。
删除记录前需要出现确认对话框,而Android系统不支持阻塞式对话框,因而不能直接使用Messagedlg(),需要用到回调函数,详细方法参见http://blog.****.net/realbay/article/details/43268031。本示例中,删除记录事件代码如下:
void __fastcall TForm1::DeleteButtonClick(TObject *Sender)
{
_di_TInputCloseDialogProc ADialogProc = new TMyDialogProc(this); //该行要放入form()中,变量声明放入头文件,即只new一次!
MessageDlg(L"真要删除该记录吗?",
TMsgDlgType::mtInformation,
TMsgDlgButtons() << TMsgDlgBtn::mbYes << TMsgDlgBtn::mbCancel,
0, ADialogProc);
}
其中TMyDialogProc类代码如下:
class TMyDialogProc : public TCppInterfacedObject< TInputCloseDialogProc >
{
private:
TForm *myDlgForm;
public:
__fastcall TMyDialogProc(TForm *Form0) : myDlgForm(Form0){}
{
private:
TForm *myDlgForm;
public:
__fastcall TMyDialogProc(TForm *Form0) : myDlgForm(Form0){}
void __fastcall Invoke(const System::Uitypes::TModalResult AResult)
{
if(AResult == mrYes)
{
Form1->FDTable1->Delete();
}
}
};
{
if(AResult == mrYes)
{
Form1->FDTable1->Delete();
}
}
};
本文顺便记录下上面链接文中InputBox()用法的示例代码如下:
- //---------------------------------------------------------------------------
- class TMyDialogProc3 : public TCppInterfacedObject< TInputCloseBoxProc>
- {
- private:
- TForm *FForm;
- public:
- __fastcall TMyDialogProc3(TForm * AForm) : FForm(AForm){}
- void __fastcall Invoke(const System::Uitypes::TModalResult AResult, const System::UnicodeString AValue)
- {
- if(AResult == mrOk)
- {
- ShowMessage(L"修改后的文字:" + AValue);
- }
- }
- };
- //---------------------------------------------------------------------------
- void __fastcall TForm1::Button3Click(TObject *Sender)
- {
- _di_TInputCloseBoxProc ADialogProc3 = new TMyDialogProc3(this);
- InputBox(L"输入",
- L"请输入文字:",
- L"默认文字",
- ADialogProc3
- );
- }
- //---------------------------------------------------------------------------