C
C#2y ago
Immusama

✅ Help with SQLite INSERT INTO C#

Hey all, I'm running this code and I don't understand why there is no data being stored in my created table.
using System;
using System.IO;
using System.Text;
using System.Data;
using System.Data.SQLite;

public class CPHInline
{
public bool Execute()
{
// Variables
string path = @"Data Source=C:\Users\Immusama\OneDrive - Critical Fear UG (haftungsbeschränkt)\Database\Twitch.db";
string userId = CPH.GetGlobalVar<string>("userId", true);
string userName = CPH.GetGlobalVar<string>("userName", true);
string displayName = CPH.GetGlobalVar<string>("user", true);
int isSubscribed = CPH.GetGlobalVar<int>("isSubscribed", true);
int isVip = CPH.GetGlobalVar<int>("isVip", true);
int isModerator = CPH.GetGlobalVar<int>("isModerator", true);

SQLiteConnection sql_db = new SQLiteConnection(path);


sql_db.Open();
string sql = "CREATE TABLE Viewers (userId varchar(20), userName varchar(30), displayName varchar(30), isSubscribed int, isVip int, isModerator int)";
SQLiteCommand command = new SQLiteCommand(sql, sql_db);
command.ExecuteNonQuery();


SQLiteCommand sqlInsert = new SQLiteCommand ("INSERT INTO Viewers (userId, userName, displayName, isSubscribed, isVip, isModerator) VALUES (@userId, @userName, @displayName, @isSubscribed, @isVip, @isModerator)", sql_db);
sqlInsert.Parameters.AddWithValue(@"userId", userId);
sqlInsert.Parameters.AddWithValue(@"userName", userName);
sqlInsert.Parameters.AddWithValue(@"displayName", displayName);
sqlInsert.Parameters.AddWithValue(@"isSubscribed", isSubscribed);
sqlInsert.Parameters.AddWithValue(@"isVip", isVip);
sqlInsert.Parameters.AddWithValue(@"isModerator", isModerator);

sql_db.Close();

return true;
}
}
using System;
using System.IO;
using System.Text;
using System.Data;
using System.Data.SQLite;

public class CPHInline
{
public bool Execute()
{
// Variables
string path = @"Data Source=C:\Users\Immusama\OneDrive - Critical Fear UG (haftungsbeschränkt)\Database\Twitch.db";
string userId = CPH.GetGlobalVar<string>("userId", true);
string userName = CPH.GetGlobalVar<string>("userName", true);
string displayName = CPH.GetGlobalVar<string>("user", true);
int isSubscribed = CPH.GetGlobalVar<int>("isSubscribed", true);
int isVip = CPH.GetGlobalVar<int>("isVip", true);
int isModerator = CPH.GetGlobalVar<int>("isModerator", true);

SQLiteConnection sql_db = new SQLiteConnection(path);


sql_db.Open();
string sql = "CREATE TABLE Viewers (userId varchar(20), userName varchar(30), displayName varchar(30), isSubscribed int, isVip int, isModerator int)";
SQLiteCommand command = new SQLiteCommand(sql, sql_db);
command.ExecuteNonQuery();


SQLiteCommand sqlInsert = new SQLiteCommand ("INSERT INTO Viewers (userId, userName, displayName, isSubscribed, isVip, isModerator) VALUES (@userId, @userName, @displayName, @isSubscribed, @isVip, @isModerator)", sql_db);
sqlInsert.Parameters.AddWithValue(@"userId", userId);
sqlInsert.Parameters.AddWithValue(@"userName", userName);
sqlInsert.Parameters.AddWithValue(@"displayName", displayName);
sqlInsert.Parameters.AddWithValue(@"isSubscribed", isSubscribed);
sqlInsert.Parameters.AddWithValue(@"isVip", isVip);
sqlInsert.Parameters.AddWithValue(@"isModerator", isModerator);

sql_db.Close();

return true;
}
}
20 Replies
Angius
Angius2y ago
Any exceptions? My usual suspect is that the project being kept in OneDrive/Dropbox/Mega folder can screw things up sometimes Another sus thing is that you're creating multiple tables with dynamic names for some reason
Immusama
ImmusamaOP2y ago
Oh yea, I was just trying some things out, I want the table to be just "Viewers" I changed it back now, but no there are no exceptions the log tells me it run the action just fine but there is no data in my db
Angius
Angius2y ago
Maybe the fact you have @"userId" instead of "@userId" for parameters?
Immusama
ImmusamaOP2y ago
Immusama
ImmusamaOP2y ago
Angius
Angius2y ago
Another guess would be OneDrive screwing things up
Immusama
ImmusamaOP2y ago
Okay, let me see if thats the issue
Angius
Angius2y ago
And another would be that you're checking a different database than the one that gets created... somehow It'd be best to just not rely on absolute paths, so you always know where the database file is in relation to the exe
Immusama
ImmusamaOP2y ago
Okay I will change that too, thank you.
Angius
Angius2y ago
If you want to have a backup of your code, rely on Git and Github, not on OneDrive
Immusama
ImmusamaOP2y ago
It's because I usually do not stream alone, and therefor have to sync the file to my buddy
Angius
Angius2y ago
Well he can always git pull the changes Or are you doing some demented OneDrive-based version of pair programming where the files get updated live as each of you edits them?
Immusama
ImmusamaOP2y ago
I see okay Think so Imma just try to move it to desktop rq and see if something changes
Angius
Angius2y ago
Most IDEs nowadays have tools for pair programming built-in so you don't need to rely on scuffed solutions like that, just FYI
Immusama
ImmusamaOP2y ago
Okay, thank you I moved it to a different drive, also changed it to the relative path and yet while it does create the table it does not store data in it
Angius
Angius2y ago
Ah, wait, you never execute the sqlInsert command!
sqlInsert.ExecuteNonQuery();
sqlInsert.ExecuteNonQuery();
Immusama
ImmusamaOP2y ago
eternaldespair
Angius
Angius2y ago
It's always the simple things lmao
Immusama
ImmusamaOP2y ago
Oh my god, thanks a lot. I've been struggling for hours now. Appreciate it!
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server