示例#1
0
        private static void CopyTable(SqliteConnection sql, SqliteConnection sql2, DBTableDesc table)
#endif
        {
#if ANDROID
            Android.Util.Log.Error("DBLoader", "CopyTable");
#endif

            String str = CreateTableStatementForDesc(table);
            sql2.ExecuteCommand(str);

#if ANDROID
            Android.Util.Log.Error("DBLoader", "Created");
#endif

            if (sql.DatabaseObjectExists(table.Name))
            {
                RowsRet data = sql.ExecuteCommand("Select * from " + table.Name);

#if ANDROID
                Android.Util.Log.Error("DBLoader", "Select from table.name");
#endif

                List <string>      validOldFields   = new List <string>();
                List <DBFieldDesc> invalidNewFields = new List <DBFieldDesc>();

                foreach (DBFieldDesc desc in table.Fields)
                {
                    if (data.HasColumn(desc.Name))
                    {
                        validOldFields.Add(desc.Name);
                    }
                    else
                    {
                        invalidNewFields.Add(desc);
                    }
                }


                StringBuilder commandBuilder = new StringBuilder();
                commandBuilder.Append("Insert into " + table.Name + " (ID");
                int count = validOldFields.Count + 1;;
                if (!table.Primary)
                {
                    commandBuilder.Append(", OwnerID");
                    count++;
                }
                StringBuilder fieldBuilder = new StringBuilder();
                StringBuilder valueBuilder = new StringBuilder();
                foreach (string strField in validOldFields)
                {
                    fieldBuilder.Append(", " + strField);
                }
                for (int i = 1; i < count; i++)
                {
                    valueBuilder.Append(", ?");
                }

                foreach (DBFieldDesc desc in invalidNewFields)
                {
                    if (desc.Nullable == false)
                    {
                        if (desc.Type == "INTEGER")
                        {
                            fieldBuilder.Append(", " + desc.Name);
                            valueBuilder.Append(", 0");
                        }
                    }
                }

                fieldBuilder.Append(") VALUES ( ?");
                valueBuilder.Append(");");
                commandBuilder.Append(fieldBuilder);
                commandBuilder.Append(valueBuilder);
                string command = commandBuilder.ToString();

                foreach (Row row in data.Rows)
                {
                    List <object> values = new List <object>();
                    values.Add(row["ID"]);
                    if (!table.Primary)
                    {
                        values.Add(row["OwnerID"]);
                    }

                    foreach (string strField in validOldFields)
                    {
                        values.Add(row[strField]);
                    }

                    object[] objParams = values.ToArray();
                    sql2.ExecuteCommand(command, objParams);

#if ANDROID
                    Android.Util.Log.Error("DBLoader", "row command");
#endif
                }
            }
        }
示例#2
0
        private static void CopyTable(SqliteConnection sql, SqliteConnection sql2, DBTableDesc table)
#endif
        {
            String str = CreateTableStatementForDesc(table);

            sql2.ExecuteCommand(str);

            if (sql.DatabaseObjectExists(table.Name))
            {
                RowsRet data = sql.ExecuteCommand("Select * from " + table.Name);

                List <string> validOldFields = new List <string>();

                foreach (DBFieldDesc desc in table.Fields)
                {
                    if (data.HasColumn(desc.Name))
                    {
                        validOldFields.Add(desc.Name);
                    }
                }


                StringBuilder commandBuilder = new StringBuilder();
                commandBuilder.Append("Insert into " + table.Name + " (ID");
                int count = validOldFields.Count + 1;;
                if (!table.Primary)
                {
                    commandBuilder.Append(", OwnerID");
                    count++;
                }
                foreach (string strField in validOldFields)
                {
                    commandBuilder.Append(", " + strField);
                }
                commandBuilder.Append(") VALUES ( ?");
                for (int i = 1; i < count; i++)
                {
                    commandBuilder.Append(", ?");
                }
                commandBuilder.Append(");");
                string command = commandBuilder.ToString();

                foreach (Row row in data.Rows)
                {
                    List <object> values = new List <object>();
                    values.Add(row["ID"]);
                    if (!table.Primary)
                    {
                        values.Add(row["OwnerID"]);
                    }

                    foreach (string strField in validOldFields)
                    {
                        values.Add(row[strField]);
                    }

                    object[] objParams = values.ToArray();
                    sql2.ExecuteCommand(command, objParams);
                }
            }
        }