WFA_psychometric_chart.Form1_main.sqlite_database_creation C# (CSharp) Method

sqlite_database_creation() private method

private sqlite_database_creation ( ) : void
return void
        private void sqlite_database_creation()
        {

            //--lets do try catch
            try
            {
                //--This is where we are going to create all the database  and tables of sqlite
                string databasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                string databaseFile = databasePath + @"\db_psychrometric_project.s3db";

                //--new database file 
                SQLiteConnection.CreateFile(databaseFile);

                //--now lets create the tables
                SQLiteConnection m_dbConnection = new SQLiteConnection("Data Source=" + databaseFile + ";Version=3;");
                m_dbConnection.Open();

                //--building location table : tbl_building_location
                //--This one is with the zip code later zip code is removed
                //string sql = "create table tbl_building_location (selection int,ID INTEGER PRIMARY KEY AUTOINCREMENT ,country varchar(255),state varchar(255),city varchar(255),street varchar(255), ZIP int,longitude varchar(255),latitude varchar(255),elevation varchar(255),BuildingName varchar(255),EngineeringUnits varchar(255))";
                string sql = "create table tbl_building_location (selection int,ID INTEGER PRIMARY KEY AUTOINCREMENT ,country varchar(255),state varchar(255),city varchar(255),street varchar(255) ,longitude varchar(255),latitude varchar(255),elevation varchar(255),BuildingName varchar(255),EngineeringUnits varchar(255))";

                SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
                command.ExecuteNonQuery();
                //--next table geo location value : tbl_geo_location_value
                //string sql1 = "create table tbl_geo_location_value (ID int ,longitude varchar(255),latitude varchar(255),elevation varchar(255))";
                //SQLiteCommand command1 = new SQLiteCommand(sql1, m_dbConnection);
                //command1.ExecuteNonQuery();

                //--next table historical data:tbl_historical_data
                string sql2 = "create table tbl_historical_data (ID INTEGER,date_current datetime,hour_current int,minute_current int,distance_from_building varchar(255),temperature varchar(255),humidity varchar(255),bar_pressure varchar(255),wind varchar(255),direction varchar(255),station_name varchar(255))";
                SQLiteCommand command2 = new SQLiteCommand(sql2, m_dbConnection);
                command2.ExecuteNonQuery();
                //--next table tbl_temp_himidity 
                //string sql3 = "create table tbl_temp_humidity (temp int,humidity int)";
                //SQLiteCommand command3 = new SQLiteCommand(sql3, m_dbConnection);
                //command3.ExecuteNonQuery();

                string sql3 = "create table tbl_language_option (ID int, language_id int)";
                SQLiteCommand command3 = new SQLiteCommand(sql3, m_dbConnection);
                command3.ExecuteNonQuery();


                ////--next table weather related datas...
                string sql4 = "create table tbl_weather_related_values (ID INTEGER ,location varchar(255),distance_from_building varchar(255),last_update_date varchar(255),temp varchar(255),humidity varchar(255),bar_pressure varchar(255),wind varchar(255),direction varchar(255),station_name varchar(255))";
                SQLiteCommand command4 = new SQLiteCommand(sql4, m_dbConnection);
                command4.ExecuteNonQuery();



                //--Lets input some values in the tbl_building_location and in tbl_language_option default 

                string sql_input1 = "INSERT INTO tbl_building_location (selection,country,state,city,street,BuildingName,EngineeringUnits) VALUES(1, 'china','SangHai','SangHai','No.35,yi yuan garden','Default_Building','SI') ";
                SQLiteCommand commandINput5 = new SQLiteCommand(sql_input1, m_dbConnection);
                commandINput5.ExecuteNonQuery();

                //Adding to language option
                string sql_input2 = "INSERT INTO tbl_language_option (ID,language_id) VALUES(1, 1) ";
                string sql_input3 = "INSERT INTO tbl_language_option (ID,language_id) VALUES(2, 0) ";
                string sql_input4 = "INSERT INTO tbl_language_option (ID,language_id) VALUES(3, 0) ";

                SQLiteCommand c2 = new SQLiteCommand(sql_input2, m_dbConnection);
                c2.ExecuteNonQuery();

                SQLiteCommand c3 = new SQLiteCommand(sql_input3, m_dbConnection);
                c3.ExecuteNonQuery();
                SQLiteCommand c4 = new SQLiteCommand(sql_input4, m_dbConnection);
                c4.ExecuteNonQuery();
                
                m_dbConnection.Close();//--closing the connection


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }



        }

Usage Example

コード例 #1
0
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);



            //==Before any thign lets run this code because we need language values which is set by this =================//
            Form1_main f1 = new Form1_main();

            f1.FindPathOfBuildingDBNewVersion();
            string selectedBuildingFromT3000 = f1.BuildingSelected[0].Building_Name;//This stores the building name selected in alex part

            string            databasePath1 = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string            databaseFile1 = databasePath1 + @"\db_psychrometric_project.s3db";
            DatabaseOperation db_op         = new DatabaseOperation();

            if (File.Exists(databaseFile1))
            {
                //file exist so dont create the database


                //MessageBox.Show("File exist section");

                db_op.sqlite_database_creation_For_UpdateCondition(selectedBuildingFromT3000);
                //MessageBox.Show("finish of sqlite_database_creation_For_UpdateCondition ");

                //--This one is for checking if the data is present or not if not presnet then create
                db_op.ReadDataForBuildingSelectedFromPsychrometric(); //--We find selected building in Psychro metric database

                BuildingIdentificationClass bic = new BuildingIdentificationClass();
                if (db_op.listBuildingSelectedInfo[0].BuildingName != "")
                {
                    bic.CheckingAndUpdatingBuildingDataFromT3000ToPsychro(selectedBuildingFromT3000, db_op.listBuildingSelectedInfo[0].BuildingName);
                }
                else
                {
                    //The previous building selected in Psy is empty
                    MessageBox.Show("Internal Error no building is selected. Program will run default building settings");
                    db_op.UpdateIDOneSelectBuildingInPsychro();
                    db_op.SelectBuildingInPsychro("Default_Building");//Selecting the default building
                }
            }
            else
            {
                //MessageBox.Show("Internal database not found. Creating fresh database");
                //this.Close();
                if (selectedBuildingFromT3000 != "")
                {
                    //--sqlite new databse creation

                    if (f1.BuildingSelected[0].Building_Name != "" && f1.BuildingSelected[0].country != "" && f1.BuildingSelected[0].city != "")
                    {
                        f1.sqlite_database_creation(selectedBuildingFromT3000);
                    }
                    else
                    {
                        if (MessageBox.Show("Please enter the building location information first in T300 and then restart the application.\n Do you want to continue with the default settings", "Chose an option", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            string buildingName = "Default_Building";
                            //--Seprate code is required because first time when application starts no
                            //--Building info provided so we have to go with default settings
                            f1.sqlite_database_creationWithDefaultSettingsOnly(buildingName);//Passing BuildingName
                            //Previous code doesnot selecte default building so to select default building do this on
                            db_op.SelectBuildingInPsychro(buildingName);
                        }
                        else
                        {
                            string databasePath133 = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                            string databaseFile    = databasePath133 + @"\db_psychrometric_project.s3db";

                            if (File.Exists(databaseFile))
                            {
                                File.Delete(databaseFile);
                            }
                            Environment.Exit(0);
                            Application.Exit();
                        }//Close of else
                    }
                }
                else
                {
                    //End the application
                    MessageBox.Show("Could not create new databse.Closing!");
                    Application.Exit();//Close the application
                }
            }
            //===================End of this section=======================//

            //Show the language select dialog
            MultiLang.SelectLanguage frmLang = new MultiLang.SelectLanguage();
            //MessageBox.Show("frm lang" + frmLang);
            frmLang.LoadSettingsAndShow();
            frmLang.Dispose();
            frmLang = null;

            Application.Run(new Form1_main());
        }
Form1_main