Beispiel #1
0
    static void ExportDataTables()
    {
        if (SQLite.Instance.Open(database))
        {
            SQLiteTable table = SQLite.Instance.GetTables();
            if (table != null)
            {
                List <string> tableNames = new List <string>();
                while (table.Read())
                {
                    string tableName = table.GetByColumnName("name", "");

                    tableNames.Add(tableName);
                }
                table.Close();

                ExportDataTables(tableNames);
            }
            SQLite.Instance.Close();
        }

        else
        {
            Debug.LogError("Can't open database:" + database);
        }
    }
Beispiel #2
0
 private void OnEnable()
 {
     mTableNames.Clear();
     if (SQLite.Instance.Open(database))
     {
         SQLiteTable table = SQLite.Instance.GetTables();
         mTableNames.Add("All");
         if (table != null)
         {
             while (table.Read())
             {
                 string tableName = table.GetByColumnName("name", "");
                 mTableNames.Add(tableName);
             }
             table.Close();
         }
     }
 }
Beispiel #3
0
    private void Register(IDataTable data)
    {
        if (data == null)
        {
            return;
        }

        if (mDataTables.ContainsKey(data.name) == false)
        {
            string sql = string.Format("select * from {0}", data.name.ToString());

            SQLiteTable table = SQLite.Instance.GetTable(sql);
            if (table != null)
            {
                mDataTables.Add(data.name, data);
                data.Read(table);
                table.Close();
            }
            else
            {
                Debug.LogError("Can't find table:" + data.name);
            }
        }
    }
Beispiel #4
0
    static void ExportDataTable(string tableName, string fileName, string className)
    {
        if (string.IsNullOrEmpty(tableName))
        {
            return;
        }

        if (SQLite.Instance.IsOpen() == false)
        {
            if (SQLite.Instance.Open(database) == false)
            {
                Debug.LogError("无法打开数据库:" + database);
                return;
            }
        }
        SQLiteTable info = SQLite.Instance.GetTableInfo(tableName);

        if (info == null)
        {
            Debug.LogError("无法读取表" + tableName + "信息");
            return;
        }

        string path       = string.Format("{0}/Scripts/Data/Tables/{1}.cs", Application.dataPath, fileName);
        string definition = "";
        string read       = string.Format("\t{0} o = new {1}();\n", className, className);
        string keyName    = null;
        string keyType    = null;

        while (info.Read())
        {
            string columnName = info.GetByColumnName("name", "");
            string columnType = GetType(info.GetByColumnName("type", ""));

            if (string.IsNullOrEmpty(keyName))
            {
                keyName = columnName;
            }
            if (string.IsNullOrEmpty(keyType))
            {
                keyType = columnType;
            }

            definition += string.Format("\tpublic {0} {1};\n", columnType, columnName);
            read       += string.Format("\t\t\to.{0} = table.GetByColumnName(\"{1}\", {2});\n", columnName, columnName, GetDefaultValue(columnType));
        }

        read += string.Format("\t\t\tdic.Add(o.{0},o);\n\t\t\tlist.Add(o);", keyName);

        info.Close();


        if (File.Exists(path) == false)
        {
            string code = template.Replace("{filename}", fileName)
                          .Replace("{definition}", definition)
                          .Replace("{classname}", className)
                          .Replace("{tablename}", tableName)
                          .Replace("{read}", read)
                          .Replace("{key}", keyType);

            Debug.Log(code);

            FileEx.SaveFile(path, code);
        }
        else
        {
            string datatable = File.ReadAllText(path);
            datatable = datatable.ReplaceEx("//TABLE_DEFINITION_BEGIN", "//TABLE_DEFINITION_END", definition);
            datatable = datatable.ReplaceEx("//TABLE_READ_BEGIN", "//TABLE_READ_END", "\t\t" + read + "\n");

            FileEx.SaveFile(path, datatable);
        }

        Debug.Log("成功导出数据表:" + tableName);
    }