Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to automatically create and populate an sqlite database in android from arrays (or other form) of data
    primarykey
    data
    text
    <p>It seems like a lot of unneccessary leg-work goes into manually coding a class file for each object to be stored in a database, as well as typing out by hand a whole DatabaseHelper class with all the necessary methods, etc.</p> <p>I have been trying to find a way to generate this all automatically by providing the minimum data - the required structure of the database. This should only need to be entered once, in one location. I have tried putting this in xml, declaring it in arrays, also in code annotations in object classes, but I have not found a solution to work for me.</p> <p>As an example of roughly what I'm trying to achieve, I could provide arrays as follows describing the desired structure of my database. NB Arrays with the suffix 'Cols' describe the columns desired in that table, suffix 'Opts' describes the required sqlite constraints for those columns.</p> <pre><code>static final String[] tables = {"Processes", "Machines", "Suppliers","machinesSuppliersLink"}; static final String[] processesCols = { // tables[0] "ProcessID" , "FullName" , "ShortName" , "Machines"}; static final String[] processesOpts = { "INTEGER PRIMARY KEY AUTOINCREMENT" , "TEXT" , "TEXT" , "INTEGER, FOREIGN KEY(Machines) REFERENCES Machines (MachinesID)"}; static final String[][] processes = { processesCols , processesOpts }; static final String[] machinesCols = { // tables[1] "MachinesID" , "Manufacturer" , "MachinesName", "Process" }; static final String[] machinesOpts = { "INTEGER PRIMARY KEY AUTOINCREMENT", "TEXT", "TEXT", "INTEGER, FOREIGN KEY(Process) REFERENCES Processes (ProcessID)" }; static final String[][] machines = { machinesCols , machinesOpts }; static final String[] suppliersCols = { // tables[2] "SuppliersID", "SuppliersName", "SuppliersPhone", "SuppliersWebsite", "SuppliersAddress", }; static final String[] suppliersOpts = { "INTEGER PRIMARY KEY AUTOINCREMENT", "TEXT", "INTEGER", "TEXT", "TEXT", }; static final String[][] suppliers = { suppliersCols , suppliersOpts }; static final String[] machinesSuppliersLinkCols = { // tables[3] "machinesSuppliersLinkID", "MachinesID", "SuppliersID" }; static final String[] machinesSuppliersLinkOpts = { "INTEGER PRIMARY KEY AUTOINCREMENT", "INTEGER, FOREIGN KEY(MachinesID) REFERENCES Machines (MachinesID)", "INTEGER, FOREIGN KEY(SuppliersID) REFERENCES Suppliers (SuppliersID)", }; static final String[][] machinesSuppliersLink = { machinesSuppliersLinkCols , machinesSuppliersLinkOpts }; public static final String[][][] schema = { processes , machines , suppliers , machinesSuppliersLink}; </code></pre> <p>Would somebody please help me by outlining the steps to automatically produce the necessary DatabaseHelper, classes, etc.</p> <p>Many thanks.</p> <p>PS I have a feeling that entering the above data vy creating a classes for each table would be a better starting point but felt the point would be illustrated clearer this way.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload