SQLite db inexplicably going read only...

I hate SQL, but in this instance I think it’s a UE problem. Maybe. In fact, I don’t know anymore, totally stumped…

Anyways, I’m creating an SQLite database in c++. Atm I’m only using the Engine’s standard plugins. It doesn’t seem to matter what I do, it will be populated by its first table just fine, but as soon as I try and add a second table, suddenly she’s read only. And it’s not just in the engine it’s read only, when I open the db with a db browser, it’s read only there too.

The db is created and populated with multiple functions, but they’re being called immediately after each other in BP:

void USQLiteDB::CreateMainDatabase()
{
	sqlite3* db;
	FString dbPath = FPaths::Combine(FPaths::ProjectSavedDir(), "Current.btf");
	int OpenResult = sqlite3_open(TCHAR_TO_UTF8(*dbPath), &db);
	if (OpenResult == SQLITE_OK) {
		UE_LOG(LogTemp, Warning, TEXT("Created Database!"));
	} else {
		UE_LOG(LogTemp, Warning, TEXT("Failed to create database: %s"), ANSI_TO_TCHAR(sqlite3_errmsg(db)));
	}

	// Close the database v2
	int CloseResult = sqlite3_close_v2(db);
	if (CloseResult != SQLITE_OK)
	{
		UE_LOG(LogTemp, Warning, TEXT("At creation, failed to close database: %s"), ANSI_TO_TCHAR(sqlite3_errmsg(db)));
	}
}

And according to the output log this is fine. Then insert a table:

void USQLiteDB::CreateDB_Reference_Table()
{
	sqlite3* db;
	FString dbPath = FPaths::Combine(FPaths::ProjectSavedDir(), "Current.btf");
	int OpenResult = sqlite3_open_v2(TCHAR_TO_UTF8(*dbPath), &db, SQLITE_OPEN_READWRITE, NULL);
	if (OpenResult == SQLITE_OK) {
		UE_LOG(LogTemp, Warning, TEXT("DB_Reference opened the DB successfully in read/write mode"));
	} else {
		UE_LOG(LogTemp, Warning, TEXT("DB_Reference failed to open DB: %s"), ANSI_TO_TCHAR(sqlite3_errmsg(db)));
	}

	FString createQuery = "CREATE TABLE DB_Reference (EmpireID INTEGER, IncidentID INTEGER, NebulaID INTEGER, StellarID INTEGER, TileQ INTEGER, TileR INTEGER, TileS INTEGER, FOREIGN KEY(EmpireID) REFERENCES tbl_EmpireIdentity(EmpireID), FOREIGN KEY(NebulaID) REFERENCES tbl_NebulaIdentity(NebulaID), FOREIGN KEY(StellarID) REFERENCES tbl_StellarIdentity(StellarID), FOREIGN KEY(TileQ, TileR, TileS) REFERENCES tbl_TileIdentity(TileQ, TileR, TileS));";
	sqlite3_stmt* createStmt;
	UE_LOG(LogTemp, Warning, TEXT("DB_Reference creating table Statement"));
	sqlite3_prepare_v2(db, TCHAR_TO_UTF8(*createQuery), createQuery.Len(), &createStmt, NULL);
	if (sqlite3_step(createStmt) != SQLITE_DONE) UE_LOG(LogTemp, Warning, TEXT("DB_Reference table creation failed!"));

	// Close the database v2
	int CloseResult = sqlite3_close_v2(db);
	if (CloseResult != SQLITE_OK)
	{
		UE_LOG(LogTemp, Warning, TEXT("Reference table creator failed to close database: %s"), ANSI_TO_TCHAR(sqlite3_errmsg(db)));
	}
}

This also works just fine. I then insert another table using IDENTICAL code as above and it inexplicably fails…


It appears the db is read only - though I have absolutely no idea why… at my wits end, help me solve this and you can have my kids :smiley:

C’mon… my kids are adorable…

I figured it out :smiley: :exploding_head: :weary:

Stoopid SQL and its you have to manually close everything demands! Looks like I get to keep my kids… :crazy_face:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.