2

Query existing database and place queried data into newly created geodatabase

I have built a model that will take an existing database and query all feature classes in that dataset using a SQL Expression and I then want all the queried data from each feature class to be exported by feature class to a newly created database. I know I can manually create a new geodatabase and then set the output to that the geodatabase in the "Select" tool but I want the model to create the database automatically. The model here shows how I have tried to do it but by doing it the way illustrated here it creates a new GDB for each iteration, exports the feature to the new GDB and then moves on the next iteration, overwriting the previous feature class.

How can I create the geodatabase first (once and only once), then run the iteration and place each each iterated feature class into the newly created database?

Any advice?

  • Is this happening because you're not creating a unique output (name) at each iteration? I'm wondering if you can parse out the name of the input feature class and use it as an inline variable substitution for the export name. – Clickinaway Sep 23 '13 at 15:54
  • 2
    First, you have to move the Create Database tool, prior to the iteration function in the model. I think that right now, even though it is on a different leg, it still falls after the Iteration, and thus will be caught in the cycle. – Get Spatial Sep 23 '13 at 15:57
  • 1
    "Get Spatial" is correct, everything in a model with an iterator is executed on each cycle. Try not to think of it as a starting point and everything downstream of it is run with each cycle of the iterator, the iterator affects the WHOLE model. So if you want the create geodatabase step in a model then you have to split it out into a master model and what you have now becomes a sub-model. If it were me, I would simply create the geodatabase manually as it never changes and drop it from your model, may be place a label stating that the geodatabase must exist? – Hornbydd Sep 23 '13 at 17:15
  • And how would I do that? I've tried to move it ahead of the iteration by setting it as a precondition but after it still gets caught in they cycle. I haven't been able to figure out a way where it first creates the GDB and then moves on the iteration and FC to GDB. I'd really like to automate the database creation but if I have to create it manually, I guess it is what it is. Or go to Python. – user10444 Sep 23 '13 at 17:31
  • 1
    As I said, everything gets run in a model with an iterator, it affects the WHOLE model. Placing it as a precondition is pointless as EVERYTHING gets runs in the WHOLE model. If you want the create geodatabase within a model then you need to split it into a master with a sub-model. I suggest you look at the help file on creating sub-models, its the only way you'll do it. Personally I would drop it, keeping the model nice and simple. – Hornbydd Sep 23 '13 at 22:40
2

I recently deleted my post regarding adding a precondition between the creation of the Personal GDB and the iteration step. As @Hornbydd points out, anything within the model when an iteration is also present will run every time. This is noted in the online help: Iterate Feature Classes (ModelBuilder) (last bullet point).

I created a quick script within a toolbox that accepts three model parameters. I did not add the Select (Analysis) tool to the script...but I am sure that it can be done. I have tested this script and it creates the new GBD and adds the feature classes present in the input workspace.

import arcpy
import os

GDB_name = arcpy.GetParameterAsText(0)
GDB_Location = arcpy.GetParameterAsText(1)

arcpy.CreatePersonalGDB_management(GDB_Location, GDB_name)

arcpy.env.workspace = arcpy.GetParameterAsText(2)

fcs = arcpy.ListFeatureClasses()

feature = []

for fc in fcs:
    feature.append(fc)

arcpy.FeatureClassToGeodatabase_conversion(feature, os.path.join(GDB_Location,  GDB_name) + ".mdb")

Parameters of tool

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.