i'v used this code to generate sqlite Database ..
private static SQLiteConnection GenData()
{
SQLiteConnection.CreateFile("MyDatabase.sqlite");
SQLiteConnection m_dbConnection;
m_dbConnection new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
m_dbConnection.Open();
string createTableQuery = @"CREATE TABLE IF NOT EXISTS [MyTable] (
[ID] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
[Value] VARCHAR(2048) NULL
)";
SQLiteCommand command = new SQLiteCommand(createTableQuery, m_dbConnection);
command.ExecuteNonQuery();
return m_dbConnection;
}
and i use it in button click to insert Data and Create DB
private void insertintoDB(string Textbox)
{
SQLiteConnection m_dbConnection = GenData();
string sql = "insert into MyTable (Value) values ('" + Textbox + "')";
SQLiteCommand commmand = new SQLiteCommand(sql, m_dbConnection);
commmand.ExecuteNonQuery();
}
insertintoDB(textbox1.text)
and i put GenData()
in load event
but every time i use the application .. it auto Generate the database and i need it to be only one time
Do a simple file check at the location your database is getting created at (typically this is App_Data, though you may want to specify a different location using the appropriate environment variables).
Then create a conditional statement to execute you "CreateFile" command only if the file doesn't exist.
Just add an if statement before your Create File
private static SQLiteConnection GenData()
{
if (!System.IO.File.Exists("MyDatabase.sqlite"))
SQLiteConnection.CreateFile("MyDatabase.sqlite");
.....
This will check whether a file exists before creating it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.