Search code examples
pervasivepervasive-sql

Create Database in PervasiveSQL from Command Line


How do I create a database in PervasiveSQL using the command line.

I know how to do it via Control Center, but I would rather create it via the command line. I am working to automate the standup of a PervasiveSQL box for a project I am working on. I have the server install happening silently and I am adjusting the Server Configuration using a RegKey import.

Now i just need to script the creation of the database. The new database will use existing database files which are already copied to the server.

In the documentation I am using found here: there is a utility called dbMaint (page 264) which seems like it would do the job, but I do not seem to have that tool on my server.

Thank you in advance for your help.


Solution

  • dbMaint is only provided for PSQL on Linux. There is a way to write a utility using the Distributed Tuning Interface (DTI) or Distributed Tuning Object (DTO) to create the database. I can't link to the PSQL documentation but there is a PSQL_DTI_GUIDE.pdf and PSQL_DTO_Guide.pdf in the PSQL v11 documentation download that describes how to use those APIs.

    Found a C# sample I put together a while back. The Pervasive DTO library will need to be added as a reference. It's a COM object. The simple sample is:

    using System;
    using DTOLib;
    
    namespace dtoTest
    {
        class Class1
        {
            [STAThread]
            static void Main(string[] args)
            {
                string compName = null;
                string userName = null;
                string password = null;
                string dbname = null;
                string ddflocation = null;
                string datalocation = null;
                dtoDbFlags dbflags = DTOLib.dtoDbFlags.dtoDbFlagDefault;
    
                DTOLib.dtoResult result;
                if (args.LongLength < 1)
                {
                    Console.WriteLine("Invalid options.\n");
                    Console.WriteLine("Usage: dtoDBN.EXE <computername> <username> <password> <dbname> <ddf location> <data location> <DBFlage>");
                    Console.WriteLine("NOTE: locations must be relative to the computer where the PSQL engine is running.");
                    Console.WriteLine("DB Flags must be passed as integer in this example with these values:  ");
                    Console.WriteLine(" P_DBFLAG_BOUND =  1;  (* bound database - must have P_DBFLAG_CREATE_DDF too *)");
                    Console.WriteLine(" P_DBFLAG_RI = 2; (*relational integrity *)");
                    Console.WriteLine(" P_DBFLAG_CREATE_DDF = 4; (*create ddf flag *)");
                    Console.WriteLine(" P_DBFLAG_NA = 2147483648; (*not applicable *)");
                    Console.WriteLine(" P_DBFLAG_DEFAULT = (P_DBFLAG_BOUND or P_DBFLAG_RI); ");
    
                    return;
                }
                if (args.LongLength == 7)
                {
                    compName = args[0].ToString();
                    userName = args[1].ToString();
                    password = args[2].ToString();
                    dbname = args[3].ToString();
                    ddflocation = args[4].ToString();
                    datalocation = args[5].ToString();
                    dbflags = (dtoDbFlags)Convert.ToInt32(args[6]);
                }
                Console.WriteLine("Create Pervasive Database using DTO and C#");
                DtoSession mDtoSession = new DTOLib.DtoSession();
                try
                {
                    result = mDtoSession.Connect(compName, userName, password);
                    if (result != 0)
                    {
                        Console.WriteLine("Error connecting to server.  Error code:");
                    }
                    else
                    {
                        //Create a Database name here.  
                        DtoDatabase db = new DtoDatabase();
                        db.Name = dbname;
                        db.DdfPath = ddflocation;
                        db.DataPath = datalocation;
                        db.Flags = dbflags;
                        result = mDtoSession.Databases.Add(db);
                        if (result !=0)
                        {
                            Console.WriteLine(string.Format("Error creating the datbase ({0}).  Error code: {1}", dbname, result.ToString()));
                        }
                        else
                        {
                            Console.WriteLine(string.Format("Database ({0}) created. ", dbname));
    
                        }
    
                        result = mDtoSession.Disconnect();
                        Console.ReadLine();
                    }
                }
                catch (Exception e1)
                {
                    Console.WriteLine(e1.Message.ToString());
                }
            }
        }
    }