简体   繁体   English

(C ++)需要数据库帮助

[英](C++) Need help with database

I'm trying to make a database and so far, I've been using strings to store my entries from a text file into an array, but this just isn't working out. 我正在尝试建立一个数据库,到目前为止,我一直在使用字符串将我的条目从文本文件存储到数组中,但这还是行不通的。 Thus, I began thinking of a new way of doing it. 因此,我开始考虑一种新的方法。

What I want to do: 我想做的事:

Lets say I have a text file with the following database... 可以说我有一个包含以下数据库的文本文件...

John Smith 00001 jsmith@email pw1 约翰·史密斯(John Smith)00001 jsmith @ email pw1

Rob Deniro 00002 rdeniro@email pw2 罗布·德尼罗(Rob Deniro)00002 rdeniro @ email pw2

Al Pacino 00003 apacino@email pw3 Al Pacino 00003 apacino @ email pw3

Joe Pesci 00004 jpesci@email 307 pw4 乔·佩西(Joe Pesci)00004 jpesci @ email 307 pw4

Joaq Phoenix 00005 jphoe@email 208 pw5 Joaq Phoenix 00005 jphoe @ email 208 pw5

John Madden 00006 jmadden@email 708 pw6 约翰·麦登00006 jmadden @ email 708 pw6

Alright, so basically what I'm stuck at is making this "inheritance" friendly. 好吧,所以基本上我坚持要使这种“继承”变得友好。 What's the best way to go about storing each entry? 存储每个条目的最佳方法是什么? Individual strings? 个别的琴弦? I've been thinking that the best way is to store each individual character until a whitespace occurs and then storing it into a string, but I'm not sure how that could be done. 我一直认为最好的方法是存储每个字符,直到出现空格,然后将其存储到字符串中,但是我不确定该怎么做。

As TomWij says, you do ifstream then strtok, but I'd recommend you escape your strings with "", not just spaces, that way you can store "something like this, for example a note about the user", that's how its done with CSV (comma separated values). 正如TomWij所说的,先执行ifstream然后进行strtok,但我建议您使用“”而不是空格来转义字符串,这样就可以存储“类似这样的内容,例如关于用户的注释”,这就是完成的过程CSV(逗号分隔值)。

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;

void readCSV(std::istream &input, std::vector< std::vector<std::string> > &output)
{
    std::string csvLine;
    // read every line from the stream
    while( std::getline(input, csvLine) )
    {
        std::istringstream csvStream(csvLine);
        std::vector<std::string> csvColumn;
        std::string csvElement;
        // read every element from the line that is seperated by commas
        // and put it into the vector or strings
        while( std::getline(csvStream, csvElement, ',') )
        {
            csvColumn.push_back(csvElement);
        }       
        output.push_back(csvColumn);
    }
}

int main()
{
    std::fstream file("file.csv", ios::in);
    if(!file.is_open())
    {
        std::cout << "File not found!\n";
        return 1;
    }
    // typedef to save typing for the following object
    typedef std::vector< std::vector<std::string> > csvVector;
    csvVector csvData;

    readCSV(file, csvData);
    // print out read data to prove reading worked
    for(csvVector::iterator i = csvData.begin(); i != csvData.end(); ++i)
    {
        for(std::vector<std::string>::iterator j = i->begin(); j != i->end(); ++j)
        {
            std::cout << *j << ", ";
        }
        std::cout << "\n";
    }
}

Which brings me to the real solution, why don't you use a CSV library like CSV module or better yet, SQLite , SQLite is dead easy to install and use, and its all around better than coding a database by hand, besides it shouldn't take you more than 1 hour to get SQLite into your codebase, since its API is REALLY easy. 这给我带来了真正的解决方案,为什么不使用CSV模块之类的CSV库或更佳的工具, SQLite ,SQLite却非常易于安装和使用,并且比起手动编写数据库要好得多,除此之外,由于SQLite的API非常简单,因此您花费不超过1个小时即可将SQLite导入代码库。

#include <stdio.h>
#include <sqlite3.h>

static int callback(void *NotUsed, int argc, char **argv, char **azColName){
  int i;
  for(i=0; i<argc; i++){
    printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
  }
  printf("\n");
  return 0;
}

int main(int argc, char **argv){
  sqlite3 *db;
  char *zErrMsg = 0;
  int rc;

  if( argc!=3 ){
    fprintf(stderr, "Usage: %s DATABASE SQL-STATEMENT\n", argv[0]);
    exit(1);
  }
  rc = sqlite3_open(argv[1], &db);
  if( rc ){
    fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
    sqlite3_close(db);
    exit(1);
  }
  rc = sqlite3_exec(db, argv[2], callback, 0, &zErrMsg);
  if( rc!=SQLITE_OK ){
    fprintf(stderr, "SQL error: %s\n", zErrMsg);
    sqlite3_free(zErrMsg);
  }
  sqlite3_close(db);
  return 0;
}

Usually the term "database" is reserved to the standard notion of a relational database such as MySQL, MS SQL Server, Oracle etc' 通常,术语“数据库”保留给关系数据库的标准概念,例如MySQL,MS SQL Server,Oracle等。

So this begs the question why don't you use a standard relational database? 因此,这就引出了一个问题,为什么不使用标准的关系数据库?

you might want to try looking up TinySQL 您可能想尝试查找TinySQL

 #include<iostream>
 #include<conio.h>
 #include<fstream>
 using namespace std;




int main(int argc, char *argv[])



  {

     char Name[100];

     char FTE[100];
      cout<<"What is the file name?\n";
      cin>>FTE;

   ifstream myfile (FTE);     

   while(1)
    {


    myfile.getline(Name, 30, '|');
    cout<<line;
   cin.ignore();


 }
  }

all this does is read all your text entries seperated by the '|' 这一切都是读取您所有用'|'分隔的文本条目 Character. 字符。 Good for making Flat File Databases in C++ and other using same methods. 适用于在C ++和其他使用相同方法的情况下制作平面文件数据库。

Read one line at a time with ifstream and then use strtok to split each line, use the whitespace as the delimiter. 使用ifstream一次读取一行,然后使用strtok拆分每一行,使用空白作为定界符。 You should use string except for the numeric values, which you can save as an int or a long if you need to support big values. 您应该使用string (数字值除外),如果需要支持大值,可以将其另存为intlong It's also more wise to store the passwords in an encrypted way if you want security. 如果需要安全性,以加密方式存储密码也是更明智的选择。

You might want to look for another solution to store your data instead of plain text for various reasons: Space, performance, security, ... It might be a good idea to try to use binary database files. 出于各种原因,您可能想要寻找另一种解决方案来存储数据而不是纯文本:空间,性能,安全性……尝试使用二进制数据库文件可能是一个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM