我如何使用结构创建数据库表 [英] How can I create database tables using structures
问题描述
我想创建并使用C接口存储数据库。我有一个包含一些变量和数据类型的结构表。如何将它们转换成数据库表。的细节如下所述。在,database.c文件,我初始化CREATETABLE功能和Folder_table结构,它包含的约束和数据类型,我必须连接到数据库中火鸟函数。有一次,我看这个结构,我想知道我怎么可以转换这个结构成表,并将其存储在数据库中。 (有什么我可以做的sprintf?)
** database.c:**
#包括/Library/Frameworks/Firebird.framework/Versions/A/Headers/ibase.h
#包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;无效* CREATETABLE(字符* tableName值,UINT行)
{
INT误差= 0;
无效*表; ////分配表
表=分配(GetTableSize(tableName值)*行); ////名单表指针
为(UINT ROWNUMBER = 0; ROWNUMBER&下;行; ROWNUMBER ++)
{
////添加code到连接链表行指针
} 返回错误;
}typedef结构Folder_Table //表类型名被_Table添加到SQLite的名字
{
////字段//注释添加到单独的领域,关系和元数据
INT folderID; //字段被添加为SQLite中列出的类型和长度的元件
浮FIELD1;
诠释域2;
浮FIELD3;
CHAR字段4 [40]; //字符串长度'40'是从现场的SQLite设置查询} Folder_Table;
INT SQLOpen(无效)
{
焦炭logInData [256];
短BufferLength中;
sprintf的(logInData,%C%C%C%C%C%C%S%C%C%S,isc_dpb_version1,
isc_dpb_num_buffers,
1,
90,
isc_dpb_user_name,
strlen的(SYSDBA),
SYSDBA
isc_dpb_password,
的strlen(masterkey),
主密钥); BufferLength中= strlen的(logInData);
如果(isc_attach_database(status_vector,strlen的(DATABASE_PATH),DATABASE_PATH,&安培; DatabaseHandle,BufferLength中,logInData))
{ isc_print_status(status_vector); }
返回0;}
** main.c中:**
#包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括/Library/Frameworks/Firebird.framework/Versions/A/Headers/ibase.hCHAR查询[] =SELECT * FROM Folder_Table WHERE ID = 3;
INT主(INT ARGC,CHAR *的argv [])
{
SQLOpen();
Folder_Table * myTable的;
myTable的= CREATETABLE(Folder_Table,ONE_ROW);}
您已经alread有了一个良好的开始定义folder_table布局。如果这是你的表需要看起来像你的数据是什么,你有做过。这里没有魔法。你只需要结构成员写出来,希望在您为表定义的字段顺序使用SQL INSERT命令的表格。
目前,您的 folder_table
功能和使用的sprintf似乎简单地收集你的所有登录信息到 logInData code>缓冲,然后附着到数据库。如果连接成功,那么你需要一个遍历所有的结构化数据进行迭代,并使用SQL
插入
命令来将数据插入到表中。你没有code表示还没有。
您可以probaly做点什么simlar插入结构值本身。您将需要以类似的方式来创建你的SQL INSERT语句。东西,将建立类似的缓冲拿着东西INSERT INTO表名(FIELD1,FIELD2,FIELD3,FIELD4)VALUES('VALUE1','VALUE2','VALUE3',VALUE4');
的同样附加到你的数据库,并为每个结构元素执行该SQL命令。
您可以继续使用火鸟,或使用的SQLite本身提供的C-API。我没有用火鸟,所以我不知道会有多大的麻烦相比的供应SQLite的API,但SQLite的C-API是比较简单和直接的。
由于FO语法,简单地指 介绍了SQLite的C / C ++接口 使用SQLite的连接器,或者看看 火鸟连接和API 火鸟连接。
首先,确保你已经定义了数据库和表布局的之前拿起键盘,并试图code吧。大部分的混乱似乎来自一个事实,即你不知道你想你的表包含您的结构化数据看起来像什么。如果你的结构Folder_Table
是你想要的定义,然后用你的表中创建,循环通过你的结构化数据和写入成员出在每个迭代中插入命令。
有大量的例子来帮助你实现你的表后,你定义它是什么样子。 SQLite的C / C ++教程 。
这可能是最好的路线图,我可以给基于这样它看起来像你和你的code。在你的结构成员映射到你的插入
语句的工作,那么如果你遇到问题,你可以问一个更具体的问题(如为什么我得到一个错误做...)
I am trying to create and store databases using c interface. I have a structure table that contains some variables and datatypes. How would I convert them into database tables. The details are mentioned below. In, database.c file, I initialized createTable function and Folder_table structure that contains the constraints and data types and I have a function to connect to the database in firebird. Once I read this structure, I would like to know how can I convert this structure into table and store it in the database. (Is there something I can do with sprintf?)
**database.c:**
#include "/Library/Frameworks/Firebird.framework/Versions/A/Headers/ibase.h"
#include <stdio.h>
#include <string.h>
void* CreateTable(char *tableName, uint rows)
{
int error = 0;
void *table;
//// Allocate table
table = Allocate(GetTableSize(tableName)*rows);
//// List table pointers
for(uint rowNumber=0; rowNumber < rows; rowNumber++)
{
//// Add code to Connect linked list row pointers
}
return error;
}
typedef struct Folder_Table // Table type name gets "_Table" added to the SQLite name
{
//// Fields // Comments are added to seperate the fields, Relationships, and metadata
int folderID; // Fields are added as elements of the type and length listed in SQLite
float field1;
int field2;
float field3;
char field4[40]; // string length '40' is queried from SQLite settings for field
} Folder_Table;
int SQLOpen(void)
{
char logInData [256];
short bufferLength;
sprintf(logInData, "%c%c%c%c%c%c%s%c%c%s", isc_dpb_version1,
isc_dpb_num_buffers,
1,
90,
isc_dpb_user_name,
strlen("SYSDBA"),
"SYSDBA",
isc_dpb_password,
strlen("masterkey"),
"masterkey");
bufferLength = strlen(logInData);
if (isc_attach_database(status_vector, strlen(DATABASE_PATH), DATABASE_PATH, &DatabaseHandle,bufferLength, logInData))
{
isc_print_status(status_vector);
}
return 0;
}
**main.c:**
#include <stdio.h>
#include<string.h>
#include "/Library/Frameworks/Firebird.framework/Versions/A/Headers/ibase.h"
char Query[] = "SELECT * FROM Folder_Table WHERE ID = 3";
int main(int argc, char *argv[])
{
SQLOpen();
Folder_Table *myTable;
myTable = CreateTable("Folder_Table", ONE_ROW);
}
You have alread made a good start defining your folder_table layout. If that is what your table needs to look like for your data, you have that done. There is no magic here. You simply need to write the structure members out to the table using the SQL INSERT command in the field order you have defined for your table.
Currently, your folder_table
function and use of sprintf seems to simply be collecting all your login information into the logInData
buffer and then attaching to the database. If you successfully attach, then you need a loop to iterate through all your structure data and use the SQL INSERT
command to insert that data into your table. You have no code for that yet.
You can probaly do something simlar for inserting the structure values themselves. You will need to create your SQL insert statements in a similar way. Something that will build a buffer holding something similar to "INSERT INTO tablename (FIELD1, FIELD2, FIELD3, FIELD4) VALUES ('VALUE1', 'VALUE2', 'VALUE3', VALUE4');"
The likewise attach to your database and execute that sql command for each structure element.
You can either continue with firebird, or use the C-API provided by sqlite itself. I haven't used firebird, so I don't know how much trouble it will be compared to the supplied SQLite API, but the SQLite C-API is relatively simple and straight forward.
As fo the syntax, simply refer to An Introduction To The SQLite C/C++ Interface to use the SQLite connector, or take a look at Firebird Connectivity and API for firebird connectivity.
Above all, make sure you have defined your database and table layout before picking up a keyboard and trying to code it. Much of the confusion seems to come from the fact that you don't know exactly what you want your table containing your structure data to look like. If your struct Folder_Table
is the definition you want, then with your table created, loop through your structure data and write the members out with the insert command on each iteration.
There are numerous examples to help you implement your table after you define what it will look like. SQLite C/C++ Tutorial.
That is probably the best road-map I can give based on where it looks like you are with your code. Work on mapping your structure member into your INSERT
statements, then if you get stuck, you can ask a more specific question (e.g. Why do I get an error doing...)
这篇关于我如何使用结构创建数据库表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!