Iluvme
Iluvme
Explore posts from servers
CC#
Created by Iluvme on 5/1/2024 in #help
Connection Error SQLite and C#
(I know it is good practice though)
21 replies
CC#
Created by Iluvme on 5/1/2024 in #help
Connection Error SQLite and C#
yup seen that, tbh, I'm busy trying to make the program even work before it being secure and it's only for my usage so
21 replies
CC#
Created by Iluvme on 5/1/2024 in #help
Connection Error SQLite and C#
I am, but I don't get it
21 replies
CC#
Created by Iluvme on 5/1/2024 in #help
Connection Error SQLite and C#
I know I am opening / closing the db but it doesn't await between each call if that makes any sense
21 replies
CC#
Created by Iluvme on 5/1/2024 in #help
Connection Error SQLite and C#
yes, that's what I am asking
21 replies
CC#
Created by Iluvme on 5/1/2024 in #help
Connection Error SQLite and C#
I am not running ide in Admin mode no
21 replies
CC#
Created by Iluvme on 5/1/2024 in #help
Connection Error SQLite and C#
I do open / close too fast, though, I tried awaiting it all before each "call" but it still doesn't "await"
21 replies
CC#
Created by Iluvme on 5/1/2024 in #help
Connection Error SQLite and C#
code = Busy (5), message = System.Data.SQLite.SQLiteException (0x87AF00AA): database is locked
database is locked
code = Busy (5), message = System.Data.SQLite.SQLiteException (0x87AF00AA): database is locked
database is locked
21 replies
CC#
Created by Iluvme on 5/1/2024 in #help
Connection Error SQLite and C#
so basically, as I understand the error given which is !
21 replies
CC#
Created by Iluvme on 5/1/2024 in #help
Connection Error SQLite and C#
using System.Data.SQLite;
using System;
using System.IO;

namespace URL {

class UrlShortener {
private readonly string connectionString = "Data Source=UrlDB.sqlite;Version=3;";

public string Shorten(string url)
{
CreateDbIfNotExist();
Guid id = Guid.NewGuid();
string surl = $"http://localhost:3000/{id}";
InsertIntoDb(url, surl);
return surl;
}

// make sure connections are closed before opening them again !
private async Task<object?> ExecuteQuery(string query)
{

try
{

SQLiteConnection udb_con = new SQLiteConnection(this.connectionString);
await udb_con.OpenAsync();
SQLiteCommand command = new SQLiteCommand(query, udb_con);

object? temp = await command.ExecuteScalarAsync();
await udb_con.CloseAsync();
return temp;
}
catch (Exception ex)
{
Console.WriteLine(ex);
}

return null;
}

private async void InsertIntoDb(string url, string shortened)
{
string record = $"INSERT INTO urls (dest, short) VALUES ('{url}', '{shortened}')";

var result = await ExecuteQuery(record);
if (result == null)
{
return;
}
return;
}

private string CreateDbIfNotExist()
{
var file = File.Exists("UrlDB.sqlite");
if (!file)
{
SQLiteConnection.CreateFile("UrlDB.sqlite");

SQLiteConnection udb_con = new SQLiteConnection(this.connectionString);
udb_con.Open();
string sql = "CREATE TABLE urls (dest varchar(512), short varchar(256))";

SQLiteCommand command = new SQLiteCommand(sql, udb_con);
command.ExecuteNonQueryAsync();
udb_con.Close();
return "Db has been correctly crated !";
}
return "Db already existed";
}
}

}
using System.Data.SQLite;
using System;
using System.IO;

namespace URL {

class UrlShortener {
private readonly string connectionString = "Data Source=UrlDB.sqlite;Version=3;";

public string Shorten(string url)
{
CreateDbIfNotExist();
Guid id = Guid.NewGuid();
string surl = $"http://localhost:3000/{id}";
InsertIntoDb(url, surl);
return surl;
}

// make sure connections are closed before opening them again !
private async Task<object?> ExecuteQuery(string query)
{

try
{

SQLiteConnection udb_con = new SQLiteConnection(this.connectionString);
await udb_con.OpenAsync();
SQLiteCommand command = new SQLiteCommand(query, udb_con);

object? temp = await command.ExecuteScalarAsync();
await udb_con.CloseAsync();
return temp;
}
catch (Exception ex)
{
Console.WriteLine(ex);
}

return null;
}

private async void InsertIntoDb(string url, string shortened)
{
string record = $"INSERT INTO urls (dest, short) VALUES ('{url}', '{shortened}')";

var result = await ExecuteQuery(record);
if (result == null)
{
return;
}
return;
}

private string CreateDbIfNotExist()
{
var file = File.Exists("UrlDB.sqlite");
if (!file)
{
SQLiteConnection.CreateFile("UrlDB.sqlite");

SQLiteConnection udb_con = new SQLiteConnection(this.connectionString);
udb_con.Open();
string sql = "CREATE TABLE urls (dest varchar(512), short varchar(256))";

SQLiteCommand command = new SQLiteCommand(sql, udb_con);
command.ExecuteNonQueryAsync();
udb_con.Close();
return "Db has been correctly crated !";
}
return "Db already existed";
}
}

}
21 replies
CC#
Created by Iluvme on 5/1/2024 in #help
Connection Error SQLite and C#
it's coming it's coming
21 replies