How can I fetch data synchronously from cordova-sqlite?

前端 未结 3 1723
灰色年华
灰色年华 2021-01-06 07:07

Is it possible to fetch data synchronously from cordova-sqlite?

I have a table caseTable with fields (ID, caseName, date). Each row in that table corr

3条回答
  •  滥情空心
    2021-01-06 07:44

    I don't believe it is possible to do this synchronously, and it's probably not a good idea to try.

    In this case, you can probably get the value you are after using a subquery, something like:

    SELECT *, 
        (SELECT COUNT(*) FROM CaseTableDetail WHERE CaseTableDetail.CaseID = CaseTable.id)
            AS CaseCount
    FROM CaseTable;
    

    (this is just a guess as you haven't specified your full table structure for the CaseName table)

    Edit:

    For the above to work, you will need a proper relational structure, rather than be adding tables dynamically. You should have only 2 tables, I'm going to call them CaseTable and CaseDetailTable.

    CaseTable is exactly what you already have.

    CaseDetailTable is similar to the Test and Test2 tables above, but has an extra field, CaseID Now I have two more table in DB Test , Test2..

       ID    CaseID      DocumentName    Date        Notes
        1         1      ppp             7/33        asdhdfkdshf
        2         1      asdjhad         9/44        dfjasgfsjfj
        3         2      sad              7/4        asdhdfkdshf
        4         2      assd            3/44        hhhhhh
        5         2      asd             2/22        adgjad
    

    So the CaseID field is a pointer to the entry in the CaseTable that each row is part of. Using WHERE, JOIN and subqueries like the one I used above, you will be able to access all the data much more efficiently. You can tell SQLite that this is what you are doing by using the REFERENCES keyword. This will tell the database to create indexes to make looking up CaseDetails faster, and it will make sure that you cannot add any rows to CaseDetailTable unless you have a corresponding entry in CaseTable.

    You can create the CaseDetailTable as follows:

       CREATE TABLE CaseDetailTable (
           id INTEGER PRIMARY KEY AUTOINCREMENT,
           CaseID INTEGER REFERENCES CaseTable (ID),
           Notes  TEXT unique NOT NULL,
           DocumentName INTEGER,
           Date TEXT NOT NULL
       );
    

提交回复
热议问题