runCommand()
CREATE TABLE IF NOT EXISTS store (id INTEGER UNIQUE PRIMARY KEY AUTOINCREMENT, code TEXT UNIQUE NOT NULL, name TEXT NOT NULL, price TEXT NOT NULL, count INTEGER);
INSERT INTO store ('code', 'name', 'price', 'count') VALUES('6273840263', 'brick', 75, 26);
selectCommand() use when i want one result
SELECT user from users WHERE tag_id = "{stringValue}";
selectCommandList() use when i want multiple results
SELECT * from users WHERE userName = '{stringValue}';
SELECT * from store WHERE code = {intValue};
DbWorker.cs
using Microsoft.Data.Sqlite;
using System;
using System.Collections.Generic;
namespace Herko.RFID
{
//DbWorker - wrapper for Microsoft.Data.Sqlite;
//(c) DEJVOSS Productions 2025;
public class Database
{
public SqliteConnection connection = null;
public Database(String databaseName)
{
connection = new SqliteConnection($"Data source={databaseName}");
connection.Open();
}
//runs a command without any return value - ex. INSERT, CREATE
public void runCommand(String commandString)
{
if (connection == null)
throw new Exception("Not connected to a dabase!");
SqliteCommand command = connection.CreateCommand();
command.CommandText = commandString;
command.ExecuteNonQuery();
}
//runs a command with a return value - ex. SELECT
public object selectCommand(String commandString)
{
if (connection == null)
throw new Exception("Not connected to a dabase!");
SqliteCommand command = connection.CreateCommand();
command.CommandText = commandString;
return command.ExecuteScalar();
}
//returls whole table as a list
public List selectCommandList(String commandString)
{
if (connection == null)
throw new Exception("Not connected to a dabase!");
SqliteCommand command = connection.CreateCommand();
command.CommandText = commandString;
command.CommandType = System.Data.CommandType.Text;
SqliteDataReader r = command.ExecuteReader();
List final = new List();
while (r.Read())
{
String[] oneRow = new String[r.FieldCount];
for (int i = 0; i < r.FieldCount; i++)
{
oneRow[i] = $"{r[i]}";
//Debug.WriteLine("TEST" + r[i]);
}
final.Add(oneRow);
}
return final;
}
}
}