SQLite several tables with same data type?

77 Views Asked by At

Currently when I insert a new object in my database, it goes to the same table based on the data type.

For example:

sqlDatabase.Insert(myObject);   // -> goes to MyObject table.

Is it possible to have several table using the same data type?

For example

sqlDatabase.Insert(myObject, "table1");
sqlDatabase.Insert(myObject, "table2");

Thanks for your help

EDIT:

I am using the sqlite-net package in a .NET 8 MAUI application: https://github.com/praeclarum/sqlite-net

Here is the full code of the class where I use the database:

public sealed class BoxingTimerDatabase
{
    private SQLiteAsyncConnection _sqlDatabase;

    public BoxingTimerDatabase()
    {
    }

    async Task Init()
    {
        if (_sqlDatabase is not null)
        {
            return;
        }

        _sqlDatabase = new SQLiteAsyncConnection(AppConstants.DatabasePath, AppConstants.Flags);

        await _sqlDatabase.CreateTableAsync<WorkoutTimer>();
        await _sqlDatabase.CreateTableAsync<WorkoutLog>();
    }

    public async Task<List<WorkoutTimer>> GetTimersAsync()
    {
        await Init();
        return await _sqlDatabase.Table<WorkoutTimer>().ToListAsync();
    }

    public async Task<List<WorkoutLog>> GetWorkoutLogs()
    {
        await Init();
        return await _sqlDatabase.Table<WorkoutLog>().ToListAsync();
    }

    public async Task<int> LogWorkoutAsync(WorkoutLog workoutLog)
    {
        await Init();
        return await _sqlDatabase.InsertAsync(workoutLog);
    }

    public async Task<WorkoutTimer> GetTimerAsync(int id)
    {
        await Init();
        return await _sqlDatabase.Table<WorkoutTimer>().Where(i => i.ID == id).FirstOrDefaultAsync();
    }

    public async Task<int> SaveTimerAsync(WorkoutTimer item)
    {
        await Init();

        if (item.ID != 0)
        {
            return await _sqlDatabase.UpdateAsync(item);
        }
        else
        {
            return await _sqlDatabase.InsertAsync(item);
        }
    }
}
0

There are 0 best solutions below