boan
New Member
Posts: 5
|
Post by boan on May 18, 2020 12:59:25 GMT
Posts: 1
Member is Online
SimpleSQL is a very easy-to-use and easy-to-use plug-in, but I have encountered a problem now, hope to help me solve it!
I have two Unity projects, Project A and Project B. Now I need to attach the SQLite data of Project B to the SQLite database of Project A.
When I use the Execute function in simpleSQL, I don't get feedback, but if I call the Execute function repeatedly, Unity gives me an error message. But when I copy my code to SQLite Expert, a SQLite visualization tool and directly execute the code, I can get the results I want, so is simpleSQL temporarily not supporting the execution of Attach statements?
Below is my code:
public SimpleSQLManager dbManager;
public void AttachDB()
{
dbManager.Execute(Attach DataBase 'C:\Users\Administrator\Documents\Avatar Rehab\
User_Information\
Avatar Rehab Mall Login System.db' as 'Avatar Rehab Mall Login System_Backup') }
|
|
boan
New Member
Posts: 5
|
Post by boan on May 18, 2020 13:17:23 GMT
The error message Unity gave me is: database Avatar Rehab Mall Login System_Backup is already in use
But I did not see the database attach success in SQLite Expert, I am very confused now, if you can help me solve the problem, I am very grateful!
|
|
|
Post by echo17 on May 19, 2020 13:09:00 GMT
I haven't used the attach functionality personally, but I'll take a look.
|
|
|
Post by echo17 on May 19, 2020 14:01:02 GMT
I put together a simple attach database example and posted it to the Asset Store. You should be able to download it now if you want to take a look. Here is the script code that was added for Demo #9:
namespace SimpleSQL.Demos { using UnityEngine; using UnityEngine.UI; using System.Collections.Generic; using System.IO;
/// <summary> /// This script shows how you can attach a database to your base database so that you /// can perform queries between them. /// </summary> public class AttachDatabase : MonoBehaviour { /// <summary> /// This is the base database that we start with. /// In this example, it is the Fantasy database. /// </summary> public SimpleSQL.SimpleSQLManager dbManager;
/// <summary> /// The file to attach to /// </summary> public TextAsset attachDatabaseFile;
/// <summary> /// This is the name of the database we will be attaching /// to. In this example, it is the SciFi database. /// </summary> public string attachDatabaseNewFileName;
/// <summary> /// Overwrite the attach database file if it exists at the /// working directory already. /// WARNING: this will overwrite any /// data written at runtime and should only be used for /// read-only databases. /// </summary> public bool overwriteAttachDatabaseIfExists;
// reference to the gui text object in our scene that will be used for output public Text outputText;
void Start() { outputText.text = "";
// get the final path in the working directory by combining the persistentDataPath with the new file name var attachDatabaseFilePath = Path.Combine(Application.persistentDataPath, attachDatabaseNewFileName);
// extract the attach database file from the Unity project into the working directory if (!ExtractDatabase(attachDatabaseFilePath)) { // failed, exit return; }
// attach to the scifi database dbManager.Execute("ATTACH DATABASE '" + attachDatabaseFilePath + "' AS SciFi");
// query the players from the newly attached database and output them to the screen outputText.text += "Players:\n"; var players = dbManager.Query<PlayerStats>("SELECT * FROM PlayerStats"); foreach (var player in players) { outputText.text += player.PlayerName + "\n"; }
outputText.text += "\n\n";
// query the weapons from the original datbase and output them to the screen outputText.text += "Weapons:\n"; var weapons = dbManager.Query<Weapon>("SELECT * FROM Weapon"); foreach (var weapon in weapons) { outputText.text += weapon.WeaponName + "\n"; } }
/// <summary> /// This method extracts the database file from the Unity project into /// the working directory. If the file does not exists, it will copy it. /// If the file already exists, it will only copy if the overwriteAttachDatabaseIfExists /// is true. /// </summary> /// <param name="attachDatabaseFilePath"></param> /// <returns></returns> private bool ExtractDatabase(string attachDatabaseFilePath) { // check if the file already exists bool fileExists = File.Exists(attachDatabaseFilePath);
if ((overwriteAttachDatabaseIfExists && fileExists) || !fileExists) { // file does not exist or the overwrite flag is true
try { if (fileExists) { // delete the file if it already exists File.Delete(attachDatabaseFilePath); }
// extract the file data to the working path File.WriteAllBytes(attachDatabaseFilePath, attachDatabaseFile.bytes); } catch { Debug.LogError("Failed to open database at the working path: " + attachDatabaseFilePath); return false; } }
return true; } } }
|
|
boan
New Member
Posts: 5
|
Post by boan on May 20, 2020 4:08:16 GMT
Thank you very much for your help. I found the answer in the Demo you provided.
Although I still didn't see the prompt of successful database connection in SQLite Expert, but in Text in Demo, I saw the query result, a query result that can only be seen after the database connection is successful.
This shows that the database connection was successful, but there was no update in SQLite Expert or other reasons that caused it to be silent.
But no matter. I am very grateful to you for the speed of solving my problems. You are really a great plugin developer and maintainer. Thank you!
|
|
|
Post by echo17 on May 20, 2020 12:51:16 GMT
Thank you for the kind words. I'm glad you were able to get it working!
|
|