Delphi X10.2 + FireDAC 使用 SQL 语句 INSERT

Posted 思想。生活。网络

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Delphi X10.2 + FireDAC 使用 SQL 语句 INSERT相关的知识,希望对你有一定的参考价值。

// CREATE TABLE [tabusers](
// [id] INTEGER PRIMARY KEY AUTOINCREMENT,
// [username] CHAR NOT NULL,
// [userpwd] CHAR NOT NULL,
// [usernickname] CHAR,
// [useroffice] CHAR,
// [userprofiles] BLOB,
// [regdatetime] DATETIME,
// [userpermission] CHAR,
// [useronline] BOOLEAN,
// [usertablename] CHAR);

MainForm.Conn.StartTransaction;
UserManagerQuery.SQL.Clear;
UserManagerQuery.SQL.Text := ‘INSERT INTO tabusers(‘ + //
          ‘username, userpwd, usernickname, useroffice, userprofiles, regdatetime, userpermission, useronline, usertablename)‘
          + //
          ‘ VALUES (:username, :userpwd, :usernickname, :useroffice, :userprofiles, :regdatetime, :userpermission, :useronline, :usertablename)‘;

        UserManagerQuery.Params[0].AsString := edtUserName.Text;
        if Using_Default_Pwd then
          UserManagerQuery.Params[1].AsString := Default_Password
        else
          UserManagerQuery.Params[1].AsString :=
            MD5Print(MD5StringA(AnsiString(edtUserPwd.Text)));
        UserManagerQuery.Params[2].AsString := edtUserNickName.Text;
        UserManagerQuery.Params[3].AsString := edtUserOffice.Text;

        MS := TMemoryStream.Create;
        ImgUserProfiles.Picture.SaveToStream(MS);
        MS.Position := 0;
        UserManagerQuery.Params[4].LoadFromStream(MS, ftBlob);
        MS.Free;

        UserManagerQuery.Params[5].AsDateTime := Now;
        UserManagerQuery.Params[6].AsString   := edtUserPermission.Text;
        UserManagerQuery.Params[7].AsBoolean  := False;

        User_Count    := UserListBox.Items.Count;
        New_user_Name := ‘tab_user‘ + Format(‘%.3d‘, [User_Count]);;
        UserManagerQuery.Params[8].AsString := New_user_Name;

        UserManagerQuery.ExecSQL;
        MainForm.Conn.Commit;

        UserListBox.Items.Add(edtUserName.Text);

        try
          // CREATE TABLE "tab_user001"(
          // [ID] INTEGER PRIMARY KEY AUTOINCREMENT,
          // [aDate] DATE NOT NULL,
          // [aComments] CHAR,
          // [aProgress] CHAR,
          // [aNote01] CHAR,
          // [aNote02] CHAR);

          UserManagerQuery.SQL.Clear;
          strSQL := ‘CREATE TABLE ‘ + New_user_Name + ‘ (‘ +
            ‘[ID] INTEGER PRIMARY KEY AUTOINCREMENT,‘ + ‘[aDate] DATE NOT NULL,‘
            + ‘[aComments] CHAR,‘ + ‘[aProgress] CHAR,‘ + ‘[aNote01] CHAR,‘ +
            ‘[aNote02] CHAR)‘;
          UserManagerQuery.Command.Prepare(strSQL);//注意这里不能用 ExecSQL / Open() / OpenOrExecute 这类东东
          UserManagerQuery.Execute();

        except
          on E: Exception do
          begin
            ShowMessage(E.Message);
          end;
        end;

  

以上是关于Delphi X10.2 + FireDAC 使用 SQL 语句 INSERT的主要内容,如果未能解决你的问题,请参考以下文章

delphi10.2报e2010 lnconpatible types:tfdconnection and tadoconnection?

使用 FireDac 在 Delphi 中动态创建和调用存储过程的正确方法是啥?

Delphi:从 IBO 迁移到 FireDac

Delphi:FireDac 连接阻止应用程序

Delphi XE FireDac 连接池

Delphi Firedac TFDtable 开放问题