Skip to content

Instantly share code, notes, and snippets.

@eojji
Last active May 24, 2021 00:02
Show Gist options
  • Save eojji/d11b9af9f51503e4c64a9e2c05c579b7 to your computer and use it in GitHub Desktop.
Save eojji/d11b9af9f51503e4c64a9e2c05c579b7 to your computer and use it in GitHub Desktop.
using Microsoft.Data.Sqlite;
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace DataAccessLibrary
{
/// <summary>
/// https://docs.microsoft.com/en-us/windows/uwp/data-access/sqlite-databases
/// </summary>
public static class DataAccess
{
public static void InitializeDatabase(string DbName)
{
using (SqliteConnection db = new SqliteConnection($"Filename={DbName}"))
{
db.Open();
// https://www.sqlite.org/lang_createtable.html
string tableCommand = "CREATE TABLE IF NOT EXISTS " +
"LogTable (" +
"Primary_Key INTEGER PRIMARY KEY, " +
"Text_Entry TEXT)";
SqliteCommand createTable = new SqliteCommand(tableCommand, db);
try
{
createTable.ExecuteNonQuery();
}
catch (SqliteException e)
{
Debug.WriteLine("SqliteErrorCode: " + e.SqliteErrorCode + ", createTable LogTable");
}
// =====================
tableCommand = "CREATE TABLE IF NOT EXISTS " +
"DownloadFolderTable (" +
"Status INTEGER DEFAULT 0, " +
"Input TEXT NOT NULL, " +
"Name TEXT, " +
"PageToken TEXT, " +
"FolderToken TEXT)";
// Status
// 0: Input
// 1: Google
// 2: Folder
// 3: End
createTable = new SqliteCommand(tableCommand, db);
try
{
createTable.ExecuteNonQuery();
}
catch (SqliteException e)
{
Debug.WriteLine("SqliteErrorCode: " + e.SqliteErrorCode + ", createTable DownloadFolderTable");
}
// =====================
tableCommand = "CREATE TABLE IF NOT EXISTS " +
"DownloadFileTable (" +
"Status INTEGER DEFAULT 0, " +
"Id TEXT NOT NULL, " +
"Name TEXT NOT NULL, " +
"MimeType TEXT NOT NULL, " +
"Size INTEGER, " +
"ModifiedTime TEXT, " +
"CanDownload INTEGER NOT NULL, " +
"FolderToken TEXT)";
createTable = new SqliteCommand(tableCommand, db);
try
{
createTable.ExecuteNonQuery();
}
catch (SqliteException e)
{
Debug.WriteLine("SqliteErrorCode: " + e.SqliteErrorCode + ", createTable DownloadFileTable");
}
// =====================
tableCommand = "CREATE TABLE IF NOT EXISTS " +
"CopyFolderTable (" +
"Status INTEGER DEFAULT 0, " +
"Input TEXT NOT NULL, " +
"Name TEXT, " +
"PageToken TEXT, " +
"FolderId TEXT)";
createTable = new SqliteCommand(tableCommand, db);
try
{
createTable.ExecuteNonQuery();
}
catch (SqliteException e)
{
Debug.WriteLine("SqliteErrorCode: " + e.SqliteErrorCode + ", createTable CopyFolderTable");
}
// =====================
tableCommand = "CREATE TABLE IF NOT EXISTS " +
"CopyFileTable (" +
"Status INTEGER DEFAULT 0, " +
"Id TEXT NOT NULL, " +
"Name TEXT NOT NULL, " +
"MimeType TEXT NOT NULL, " +
"Size INTEGER, " +
"ModifiedTime TEXT, " +
"CanCopy INTEGER NOT NULL, " +
"FolderId TEXT NOT NULL)";
createTable = new SqliteCommand(tableCommand, db);
try
{
createTable.ExecuteNonQuery();
}
catch (SqliteException e)
{
Debug.WriteLine("SqliteErrorCode: " + e.SqliteErrorCode + ", createTable CopyFileTable");
}
// =====================
tableCommand = "CREATE TABLE IF NOT EXISTS " +
"UploadFolderTable (" +
"Status INTEGER DEFAULT 0, " +
"FutureToken TEXT NOT NULL, " +
"GetIdx INTEGER DEFAULT 0, " +
"Path TEXT, " +
"Name TEXT, " +
"FolderId TEXT)";
createTable = new SqliteCommand(tableCommand, db);
try
{
createTable.ExecuteNonQuery();
}
catch (SqliteException e)
{
Debug.WriteLine("SqliteErrorCode: " + e.SqliteErrorCode + ", createTable UploadFolderTable");
}
// =====================
tableCommand = "CREATE TABLE IF NOT EXISTS " +
"UploadFileTable (" +
"Status INTEGER DEFAULT 0, " +
"FutureToken TEXT, " +
"Path TEXT, " +
"Name TEXT, " +
"FolderId TEXT, " +
"GoogleId TEXT)";
createTable = new SqliteCommand(tableCommand, db);
try
{
createTable.ExecuteNonQuery();
}
catch (SqliteException e)
{
Debug.WriteLine("SqliteErrorCode: " + e.SqliteErrorCode + ", createTable UploadFileTable");
}
db.Close();
}
}
public static void AddData(string DbName, string inputText)
{
using (SqliteConnection db = new SqliteConnection($"Filename={DbName}"))
{
db.Open();
SqliteCommand insertCommand = new SqliteCommand
{
Connection = db,
CommandText = "INSERT INTO LogTable VALUES (NULL, @Entry);"
};
insertCommand.Parameters.Add(new SqliteParameter("@Entry", inputText));
try
{
insertCommand.ExecuteNonQuery();
}
catch (SqliteException e)
{
Debug.WriteLine("SqliteErrorCode: " + e.SqliteErrorCode + ", INSERT LogTable");
}
db.Close();
}
}
public static List<string> GetData(string DbName)
{
List<string> entries = new List<string>();
using (var connection = new SqliteConnection($"Filename={DbName}"))
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText = "SELECT Text_Entry from LogTable";
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
entries.Add(reader.GetString(0));
}
}
connection.Close();
}
return entries;
}
public static int DeleteLog(string DbName)
{
using (SqliteConnection db = new SqliteConnection($"Filename={DbName}"))
{
db.Open();
int result = 0;
SqliteCommand deleteCommand = new SqliteCommand
{
Connection = db,
CommandText = "Delete from LogTable"
};
try
{
result = deleteCommand.ExecuteNonQuery();
}
catch (SqliteException e)
{
Debug.WriteLine("SqliteErrorCode: " + e.SqliteErrorCode + ", Delete LogTable");
}
db.Close();
return result;
}
}
}
}
@eojji
Copy link
Author

eojji commented Apr 23, 2021

[https://drive.eojji.com/upload](Upload app)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment