P
P
p4p2014-08-21 20:13:39
SQLite
p4p, 2014-08-21 20:13:39

How to write a class to work with sqlite?

I am not selenium in OOP, need help.
There is a class SQLite , it is in the connect() method , it turns out an open sqlite database into the database object.
I would like to implement something like this (so that you can execute queries from other classes without recreating objects of this one) sqlite.query() something like this)
It seems to be called a singleton?
Now I work like this

SQLite sqlite = new SQLite();
sqlite.connect().query();

public class SQLite {

    public  SqliteDatabase  connect()
    {
           SqliteDatabase database = new SqliteDatabase();
           // тут куча кода
           database.Open(Application.persistentDataPath + "/db.sqlite");
           return database;
    }

}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2014-08-21
Protko @Fesor

I think it's called chaining.

public class SQLite {
    
    private  SqliteDatabase link;
    private bool isConnected;

    public  SQLite  connect()
    {
           if (isConnected) return this;
           link = new SqliteDatabase();
           // тут куча кода
           link.Open(Application.persistentDataPath + "/db.sqlite");
           isConnected = true;

           return this;
    }

}

G
gleb_kudr, 2014-08-21
@gleb_kudr

Store your public database wrapper object in some global class and refer to it when needed. Singleton is in behavior and meaning almost the same as a global variable, you don't have to bother.
Especially with SQLite - it doesn't support multithreading, so supporting more than one connection is detrimental in all cases.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question