数据库

 首页 > 数据库 > Sqlite > 关于sqlite3数据库

关于sqlite3数据库

分享到:
【字体:

//---------------------------------------------------------------------------

 

#include

#pragma hdrstop

 

#include "Unit1.h"

 

 

//---------------------------------------------------------------------------

#pragma package(smart_init)

#pragma link "ZConnection"

#pragma link "ZAbstractDataset"

#pragma link "ZAbstractRODataset"

#pragma link "ZDataset"

#pragma resource "*.dfm"

TForm1 *Form1;

//---------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)

        : TForm(Owner)

{

}

//---------------------------------------------------------------------------

void __fastcall TForm1::Button4Click(TObject *Sender)

{

this->Close();

}

//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)

{

        Memo1->Clear();

        Button2->Enabled = false;

        Button3->Enabled = false;

        Button5->Enabled = false;

        Button6->Enabled = false;

        Button7->Enabled = false;

        Button8->Enabled = false;

        name_edit->Enabled = false;

        age_edit->Enabled = false;

        Edit1->Enabled = false;

        CB1->Enabled = false;

        Label1->Enabled = false;

        Label2->Enabled = false;

        OpenDialog1->Filter = "SQLite(*.DB3,*.DB,*.SLI)|*.DB3;*.DB;*.SLI|All File|*.*";

        ZConnection1->Protocol = "sqlite-3";

        ZQuery1->Connection = ZConnection1;

        CB1->Clear();

        CB1->Items->Add("name");

        CB1->Items->Add("age");

        CB1->ItemIndex = 0 ;

        m = "";

}

//---------------------------------------------------------------------------

 

void __fastcall TForm1::Button1Click(TObject *Sender)

{

      if (OpenDialog1->Execute())

      {

        AnsiString sDBPath = OpenDialog1->FileName;

        Memo1->Lines->Add("数据库位置:");

        Memo1->Lines->Add(sDBPath);

        ZConnection1->Database = sDBPath;

       // ZConnection1->Database = "test_1.db";

        Button2->Enabled = true;

      }

}

//---------------------------------------------------------------------------

 

void __fastcall TForm1::Button2Click(TObject *Sender)

{

     ZConnection1->Connect();

     if (ZConnection1->Connected)

     {

        Memo1->Clear();

        Memo1->Lines->Add("连接成功!");

        Memo1->Lines->Add("数据库位置:");

        Memo1->Lines->Add(ZConnection1->Database);

        Button2->Enabled = false;

        Button3->Enabled = true;

        Button5->Enabled = true;

        Button6->Enabled = true;

        Button7->Enabled = true;

        Button8->Enabled = true;

        name_edit->Enabled = true;

        age_edit->Enabled = true;

        Edit1->Enabled = true;

        CB1->Enabled = true;

        Label1->Enabled = true;

        Label2->Enabled = true;

        LoadData();

 

     }

     else

     {

        Memo1->Lines->Add("连接失败!");

     }

}

//---------------------------------------------------------------------------

 

void __fastcall TForm1::LoadData()

{

    Memo1->Lines->Add("---------------------------------");

    AnsiString sql;

    sql.printf("select * from user;");

    ExecuteSQL(sql,false);

    while (!ZQuery1->Eof)

    {

        AnsiString name = ZQuery1->FieldByName("name")->AsString;

        int age =  ZQuery1->FieldByName("age")->AsInteger;

        AnsiString str = name + "      "+ IntToStr(age);

        Memo1->Lines->Add(str);

        ZQuery1->Next();

    }

}

 

 

void __fastcall TForm1::Button3Click(TObject *Sender)

{

     ZConnection1->Disconnect();

     Memo1->Clear();

     Memo1->Lines->Add("连接已断开,谢谢!");

     Button2->Enabled = true;

     Button3->Enabled = false;

     Button5->Enabled = false;

     Button6->Enabled = false;

     Button7->Enabled = false;

     Button8->Enabled = false;

     name_edit->Enabled = false;

     age_edit->Enabled = false;

     Edit1->Enabled = false;

     CB1->Enabled = false;

     Label1->Enabled = false;

     Label2->Enabled = false;

}

//---------------------------------------------------------------------------

void __fastcall TForm1::ExecuteSQL(AnsiString sql,bool bSelect)

{

 

        ZQuery1->Close();

        ZQuery1->SQL->Clear();

        ZQuery1->SQL->Add(sql);

    if (bSelect)

    {

        ZQuery1->Open();

    }

    else

    {

        ZQuery1->ExecSQL();

        ZQuery1->Active = true;

    }

 

}

 

void __fastcall TForm1::Button6Click(TObject *Sender)

{

       AnsiString name = name_edit->Text;

       AnsiString s_age = age_edit->Text;

       if (name.Trim().IsEmpty() || s_age.Trim().IsEmpty())

       {

          ShowMessage("姓名或年龄不能为空");

          return ;

       }

       int age = StrToInt(s_age);

       if ( age > 99 ||  age

       {

          ShowMessage ("年龄错误!");

          return ;

       }

       AnsiString sql;

       sql.printf("insert into user values ('%s','%d');",name,age);

       ExecuteSQL(sql,true);

       Memo1->Lines->Add("*********insert succeed*********");

       LoadData();

}

//---------------------------------------------------------------------------

 

void __fastcall TForm1::Button7Click(TObject *Sender)

{

    AnsiString name = name_edit->Text;

    AnsiString s_age = age_edit->Text;

    if (name.Trim().IsEmpty() || s_age.Trim().IsEmpty())

    {

          ShowMessage("姓名或年龄不能为空");

          return ;

    }

    int age = StrToInt(s_age);

    if ( age > 99 || age

    {

          ShowMessage ("年龄错误!");

          return ;

    }

    AnsiString sql;

    sql.sprintf("select * from user;");

    ExecuteSQL(sql, true);

    ZQuery1->Append();

    ZQuery1->FieldByName("name")->AsString = name;

    ZQuery1->FieldByName("age")->AsInteger = age;

    ZQuery1->Post();

    Memo1->Lines->Add("*********insert succeed*********");

    LoadData();

}

//---------------------------------------------------------------------------

 

 

void __fastcall TForm1::Button8Click(TObject *Sender)

{

      AnsiString sql;

      AnsiString ddata = Edit1->Text.Trim();

      if (CB1->ItemIndex == 0)

      {

        sql.printf("select * from user where [name] = '%s';",ddata);

        ExecuteSQL(sql, false);

        int n = ZQuery1->FieldByName("age")->AsInteger;

        if (n==0)

        {

                ShowMessage("没有该数据");

                return;

        }

        else

        {

              AnsiString sql_1;

              sql_1.printf("delete from user where [name] = '%s';",ddata);

              ExecuteSQL(sql_1, true);

              Memo1->Lines->Add("----------------------");

              Memo1->Lines->Add("|    删除成功!   |") ;

              Memo1->Lines->Add("----------------------") ;

              LoadData();

        }

      }

      else if (CB1->ItemIndex == 1)

      {

        if (!IsInteger(ddata))

         {

                ShowMessage("请输入数字");

                Edit1->Clear();

                return ;

         }

        int n_age = StrToInt(ddata);

        sql.printf("select * from user where [age] = '%d';",n_age);

        ExecuteSQL(sql, false);

        AnsiString  m= ZQuery1->FieldByName("name")->AsString;

        if ( m == "" )

        {

                ShowMessage("没有该数据");

                return;

        }

        else

        {

              AnsiString sql_1;

              sql_1.printf("delete from user where [age] = '%d';",n_age);

              ExecuteSQL(sql_1, true);

              Memo1->Lines->Add("----------------------");

              Memo1->Lines->Add("|    删除成功!   |") ;

              Memo1->Lines->Add("----------------------") ;

              LoadData();

        }

      }

      Edit1->Clear();

}

//---------------------------------------------------------------------------

 

 

void __fastcall TForm1::Button5Click(TObject *Sender)

{

      AnsiString sql;

      AnsiString ddata = Edit1->Text.Trim();

      if (ddata == "")

      {

        LoadData();

      }

      if (CB1->ItemIndex == 0)

      {

        int n;

        sql.printf("select * from user where [name] = '%s';",ddata);

        ExecuteSQL(sql, false);

        n = ZQuery1->FieldByName("age")->AsInteger;

        if (n == 0 && ddata != "")

        {

                ShowMessage("没有该数据");

                Edit1->Clear();

                return ;

        }

        while (!ZQuery1->Eof)

        {

              n = ZQuery1->FieldByName("age")->AsInteger;

              AnsiString str = "*   " + ddata + "     "+ IntToStr(n)+ "      *";

              Memo1->Lines->Add("***********************");

              Memo1->Lines->Add(str);

              Memo1->Lines->Add("************************");

              ZQuery1->Next();

        }

 

      }

     if (CB1->ItemIndex == 1 && ddata != "")

      {

        if (!IsInteger(ddata) )

         {

                ShowMessage("请输入数字");

                Edit1->Clear();

                return ;

         }

 

        int n_age = StrToInt(ddata);

        sql.printf("select * from user where [age] = '%d';",n_age);

        ExecuteSQL(sql, false);

        m = ZQuery1->FieldByName("name")->AsString;

        if (m == "" && ddata != "")

        {

                ShowMessage("没有该数据");

                Edit1->Clear();

                return ;

        }

        while (!ZQuery1->Eof)

        {

          m= ZQuery1->FieldByName("name")->AsString;

          AnsiString str = "*   " + m + "     "+ IntToStr(n_age)+ "      *";

          Memo1->Lines->Add("***********************");

          Memo1->Lines->Add(str);

          Memo1->Lines->Add("************************");

          ZQuery1->Next();

        }

      }

      Edit1->Clear();

}

//---------------------------------------------------------------------------

 

void __fastcall TForm1::age_editKeyPress(TObject *Sender, char &Key)

{

        AnsiString str =  age_edit->Text;

        if (Key >0x60 && Key

         Key = 0;

        if(str.Trim().Length() >= 2 && Key != 0x08 && Key != 0x2E)

        {

        Key = 0;

        }

 

}

//---------------------------------------------------------------------------

 

 

bool __fastcall TForm1::IsInteger(AnsiString s)

{

        float x;

        if (TryStrToFloat(s,x) == true)

                return true;

        else

                return false;

}

 

Unit1.h

//---------------------------------------------------------------------------

 

#include

#pragma hdrstop

 

#include "Unit1.h"

 

 

//---------------------------------------------------------------------------

#pragma package(smart_init)

#pragma link "ZConnection"

#pragma link "ZAbstractDataset"

#pragma link "ZAbstractRODataset"

#pragma link "ZDataset"

#pragma resource "*.dfm"

TForm1 *Form1;

//---------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)

        : TForm(Owner)

{

}

//---------------------------------------------------------------------------

void __fastcall TForm1::Button4Click(TObject *Sender)

{

this->Close();

}

//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)

{

        Memo1->Clear();

        Button2->Enabled = false;

        Button3->Enabled = false;

        Button5->Enabled = false;

        Button6->Enabled = false;

        Button7->Enabled = false;

        Button8->Enabled = false;

        name_edit->Enabled = false;

        age_edit->Enabled = false;

        Edit1->Enabled = false;

        CB1->Enabled = false;

        Label1->Enabled = false;

        Label2->Enabled = false;

        OpenDialog1->Filter = "SQLite(*.DB3,*.DB,*.SLI)|*.DB3;*.DB;*.SLI|All File|*.*";

        ZConnection1->Protocol = "sqlite-3";

        ZQuery1->Connection = ZConnection1;

        CB1->Clear();

        CB1->Items->Add("name");

        CB1->Items->Add("age");

        CB1->ItemIndex = 0 ;

        m = "";

}

//---------------------------------------------------------------------------

 

void __fastcall TForm1::Button1Click(TObject *Sender)

{

      if (OpenDialog1->Execute())

      {

        AnsiString sDBPath = OpenDialog1->FileName;

        Memo1->Lines->Add("数据库位置:");

        Memo1->Lines->Add(sDBPath);

        ZConnection1->Database = sDBPath;

       // ZConnection1->Database = "test_1.db";

        Button2->Enabled = true;

      }

}

//---------------------------------------------------------------------------

 

void __fastcall TForm1::Button2Click(TObject *Sender)

{

     ZConnection1->Connect();

     if (ZConnection1->Connected)

     {

        Memo1->Clear();

        Memo1->Lines->Add("连接成功!");

        Memo1->Lines->Add("数据库位置:");

        Memo1->Lines->Add(ZConnection1->Database);

        Button2->Enabled = false;

        Button3->Enabled = true;

        Button5->Enabled = true;

        Button6->Enabled = true;

        Button7->Enabled = true;

        Button8->Enabled = true;

        name_edit->Enabled = true;

        age_edit->Enabled = true;

        Edit1->Enabled = true;

        CB1->Enabled = true;

        Label1->Enabled = true;

        Label2->Enabled = true;

        LoadData();

 

     }

     else

     {

        Memo1->Lines->Add("连接失败!");

     }

}

//---------------------------------------------------------------------------

 

void __fastcall TForm1::LoadData()

{

    Memo1->Lines->Add("---------------------------------");

    AnsiString sql;

    sql.printf("select * from user;");

    ExecuteSQL(sql,false);

    while (!ZQuery1->Eof)

    {

        AnsiString name = ZQuery1->FieldByName("name")->AsString;

        int age =  ZQuery1->FieldByName("age")->AsInteger;

        AnsiString str = name + "      "+ IntToStr(age);

        Memo1->Lines->Add(str);

        ZQuery1->Next();

    }

}

 

 

void __fastcall TForm1::Button3Click(TObject *Sender)

{

     ZConnection1->Disconnect();

     Memo1->Clear();

     Memo1->Lines->Add("连接已断开,谢谢!");

     Button2->Enabled = true;

     Button3->Enabled = false;

     Button5->Enabled = false;

     Button6->Enabled = false;

     Button7->Enabled = false;

     Button8->Enabled = false;

     name_edit->Enabled = false;

     age_edit->Enabled = false;

     Edit1->Enabled = false;

     CB1->Enabled = false;

     Label1->Enabled = false;

     Label2->Enabled = false;

}

//---------------------------------------------------------------------------

void __fastcall TForm1::ExecuteSQL(AnsiString sql,bool bSelect)

{

 

        ZQuery1->Close();

        ZQuery1->SQL->Clear();

        ZQuery1->SQL->Add(sql);

    if (bSelect)

    {

        ZQuery1->Open();

    }

    else

    {

        ZQuery1->ExecSQL();

        ZQuery1->Active = true;

    }

 

}

 

void __fastcall TForm1::Button6Click(TObject *Sender)

{

       AnsiString name = name_edit->Text;

       AnsiString s_age = age_edit->Text;

       if (name.Trim().IsEmpty() || s_age.Trim().IsEmpty())

       {

          ShowMessage("姓名或年龄不能为空");

          return ;

       }

       int age = StrToInt(s_age);

       if ( age > 99 ||  age

       {

          ShowMessage ("年龄错误!");

          return ;

       }

       AnsiString sql;

       sql.printf("insert into user values ('%s','%d');",name,age);

       ExecuteSQL(sql,true);

       Memo1->Lines->Add("*********insert succeed*********");

       LoadData();

}

//---------------------------------------------------------------------------

 

void __fastcall TForm1::Button7Click(TObject *Sender)

{

    AnsiString name = name_edit->Text;

    AnsiString s_age = age_edit->Text;

    if (name.Trim().IsEmpty() || s_age.Trim().IsEmpty())

    {

          ShowMessage("姓名或年龄不能为空");

          return ;

    }

    int age = StrToInt(s_age);

    if ( age > 99 || age

    {

          ShowMessage ("年龄错误!");

          return ;

    }

    AnsiString sql;

    sql.sprintf("select * from user;");

    ExecuteSQL(sql, true);

    ZQuery1->Append();

    ZQuery1->FieldByName("name")->AsString = name;

    ZQuery1->FieldByName("age")->AsInteger = age;

    ZQuery1->Post();

    Memo1->Lines->Add("*********insert succeed*********");

    LoadData();

}

//---------------------------------------------------------------------------

 

 

void __fastcall TForm1::Button8Click(TObject *Sender)

{

      AnsiString sql;

      AnsiString ddata = Edit1->Text.Trim();

      if (CB1->ItemIndex == 0)

      {

        sql.printf("select * from user where [name] = '%s';",ddata);

        ExecuteSQL(sql, false);

        int n = ZQuery1->FieldByName("age")->AsInteger;

        if (n==0)

        {

                ShowMessage("没有该数据");

                return;

        }

        else

        {

              AnsiString sql_1;

              sql_1.printf("delete from user where [name] = '%s';",ddata);

              ExecuteSQL(sql_1, true);

              Memo1->Lines->Add("----------------------");

              Memo1->Lines->Add("|    删除成功!   |") ;

              Memo1->Lines->Add("----------------------") ;

              LoadData();

        }

      }

      else if (CB1->ItemIndex == 1)

      {

        if (!IsInteger(ddata))

         {

                ShowMessage("请输入数字");

                Edit1->Clear();

                return ;

         }

        int n_age = StrToInt(ddata);

        sql.printf("select * from user where [age] = '%d';",n_age);

        ExecuteSQL(sql, false);

        AnsiString  m= ZQuery1->FieldByName("name")->AsString;

        if ( m == "" )

        {

                ShowMessage("没有该数据");

                return;

        }

        else

        {

              AnsiString sql_1;

              sql_1.printf("delete from user where [age] = '%d';",n_age);

              ExecuteSQL(sql_1, true);

              Memo1->Lines->Add("----------------------");

              Memo1->Lines->Add("|    删除成功!   |") ;

              Memo1->Lines->Add("----------------------") ;

              LoadData();

        }

      }

      Edit1->Clear();

}

//---------------------------------------------------------------------------

 

 

void __fastcall TForm1::Button5Click(TObject *Sender)

{

      AnsiString sql;

      AnsiString ddata = Edit1->Text.Trim();

      if (ddata == "")

      {

        LoadData();

      }

      if (CB1->ItemIndex == 0)

      {

        int n;

        sql.printf("select * from user where [name] = '%s';",ddata);

        ExecuteSQL(sql, false);

        n = ZQuery1->FieldByName("age")->AsInteger;

        if (n == 0 && ddata != "")

        {

                ShowMessage("没有该数据");

                Edit1->Clear();

                return ;

        }

        while (!ZQuery1->Eof)

        {

              n = ZQuery1->FieldByName("age")->AsInteger;

              AnsiString str = "*   " + ddata + "     "+ IntToStr(n)+ "      *";

              Memo1->Lines->Add("***********************");

              Memo1->Lines->Add(str);

              Memo1->Lines->Add("************************");

              ZQuery1->Next();

        }

 

      }

     if (CB1->ItemIndex == 1 && ddata != "")

      {

        if (!IsInteger(ddata) )

         {

                ShowMessage("请输入数字");

                Edit1->Clear();

                return ;

         }

 

        int n_age = StrToInt(ddata);

        sql.printf("select * from user where [age] = '%d';",n_age);

        ExecuteSQL(sql, false);

        m = ZQuery1->FieldByName("name")->AsString;

        if (m == "" && ddata != "")

        {

                ShowMessage("没有该数据");

                Edit1->Clear();

                return ;

        }

分享到:
Ubuntu系统中安装Sqlite3数据库
安装使用数据库系统Sqlite3 安装数据库系统Sqlite3: apt-get install sqlite sqlite3 检查数据库安装结果: sqlite3 test.db .database .exit 提供下面的命令我们应该可以看到文件test.db。 ls 安装Sqlite3编译需要的工具包: apt-get install libsqlite3-dev cat rule_database.c  ...
SQLite数据库基础教程
1:sqlite常用接口 2个重要结构体和5个主要函数: sqlite3               *pdb, 数据库句柄,跟文件句柄FILE很类似 sqlite3_stmt      *stmt, 这个相当于ODBC的Command对象,用于保存编译好的SQL语句   sqlite3_open(),   打开数据库 sqlite3_exec(),   执行非查询的sql语句 sqlite3_prepa...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……