Forum Xamarin.Android

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

How can Connect Xamarin Android app for existing sqlite database?

I develop Xamarin android app. And also i create sqlite database using sqlite browser use. After insert it my existing data. My database have 5 tables. Therefore, now i want to connect it xamarin android app. How can i do it. Please help anyone.

Best Answer

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭✭
    Accepted Answer

    @SampathTharanga @xamarinx
    Ok here is it a sample where i create a db sqlLite and insert items and get it

    `
    public static bool CreateDatabase()
    {
    bool bOk = false;

            try
            {
    
                    //
                    db = new SQLiteConnection(dbPath);
                    db.CreateTable<Item>();
    
                    //
                    bOk = true;
                }
                else
                    bOk = false;
            }
            catch (SQLiteException ex)
            {
                //
                bOk = false;
    
                //
                string m = ex.Message;
            }
    
            //
            return bOk;
        }
    

    `

    my business object => it's just a sample

    `
    [Table("Items")]
    public class Item
    {
    //
    [PrimaryKey, AutoIncrement, Column("Id")]
    public int Id { get; set; }

        //
        [MaxLength(50)]
        public string Name { get; set; }
    
        //
        public DateTime CreationDate { get; set; }
    }
    

    `

    Insert element :

    `
    public static bool Insert(Item oneItem)
    {
    //
    bool bOk = false;

            try
            {
                //
                db.Insert(oneItem);
                //
                bOk = true;
            }
            catch (SQLiteException ex)
            {
                string s = ex.Message;
            }
    
            //
            return bOk;
        }
    

    `

    get items

    `

    public static List GetAll()
    {
    //
    List list= new List();

            try
            {
                list = db.Table<Item>().ToList();
            }
            catch (SQLiteException ex)
            {
                //
                string error = ex.Message;
            }
    
            //
            return list;
        }
    

    `

    hope this help

Answers

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭✭

    Hello, there are some sample on internet, youtube or in this site
    https://developer.xamarin.com/recipes/android/data/databases/sqlite/

  • SampathTharangaSampathTharanga USMember ✭✭
    edited March 2017
    @ChristopheBERNARD This is create new sqlite database. But want --> " i have a sqlite database. Then how can connect my existing database Xamarin android app". Please give a some solution.
  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭✭
    edited March 2017

    @SampathTharanga you just need to get the path where the db is locate and open the connection that all
    do something like this

    `
    private static SQLiteConnection db;
    ....
    // in your read methods get a connection
    db = new SQLiteConnection(PATH TO THE DB);

    // after => example
    List theDatas = db.Table.ToList();

    `

    normally that's all :)

  • xamarinxxamarinx SAMember ✭✭

    ChristopheBernard,
    first of all, thanks ,
    could you give us an simple example please?
    me too, I am looking for this.

  • SampathTharangaSampathTharanga USMember ✭✭
    @ChristopheBERNARD thanks friends. If you can please give us sample code. Because i am beginner.
  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭✭
    Accepted Answer

    @SampathTharanga @xamarinx
    Ok here is it a sample where i create a db sqlLite and insert items and get it

    `
    public static bool CreateDatabase()
    {
    bool bOk = false;

            try
            {
    
                    //
                    db = new SQLiteConnection(dbPath);
                    db.CreateTable<Item>();
    
                    //
                    bOk = true;
                }
                else
                    bOk = false;
            }
            catch (SQLiteException ex)
            {
                //
                bOk = false;
    
                //
                string m = ex.Message;
            }
    
            //
            return bOk;
        }
    

    `

    my business object => it's just a sample

    `
    [Table("Items")]
    public class Item
    {
    //
    [PrimaryKey, AutoIncrement, Column("Id")]
    public int Id { get; set; }

        //
        [MaxLength(50)]
        public string Name { get; set; }
    
        //
        public DateTime CreationDate { get; set; }
    }
    

    `

    Insert element :

    `
    public static bool Insert(Item oneItem)
    {
    //
    bool bOk = false;

            try
            {
                //
                db.Insert(oneItem);
                //
                bOk = true;
            }
            catch (SQLiteException ex)
            {
                string s = ex.Message;
            }
    
            //
            return bOk;
        }
    

    `

    get items

    `

    public static List GetAll()
    {
    //
    List list= new List();

            try
            {
                list = db.Table<Item>().ToList();
            }
            catch (SQLiteException ex)
            {
                //
                string error = ex.Message;
            }
    
            //
            return list;
        }
    

    `

    hope this help

  • xamarinxxamarinx SAMember ✭✭

    @ChristopheBERNARD said:
    @SampathTharanga @xamarinx
    Ok here is it a sample where i create a db sqlLite and insert items and get it

    `
    public static bool CreateDatabase()
    {
    bool bOk = false;

            try
            {
    
                    //
                    db = new SQLiteConnection(dbPath);
                    db.CreateTable<Item>();
    
                    //
                    bOk = true;
                }
                else
                    bOk = false;
            }
            catch (SQLiteException ex)
            {
                //
                bOk = false;
    
                //
                string m = ex.Message;
            }
    
            //
            return bOk;
        }
    

    `

    my business object => it's just a sample

    `
    [Table("Items")]
    public class Item
    {
    //
    [PrimaryKey, AutoIncrement, Column("Id")]
    public int Id { get; set; }

        //
        [MaxLength(50)]
        public string Name { get; set; }
    
        //
        public DateTime CreationDate { get; set; }
    }
    

    `

    Insert element :

    `
    public static bool Insert(Item oneItem)
    {
    //
    bool bOk = false;

            try
            {
                //
                db.Insert(oneItem);
                //
                bOk = true;
            }
            catch (SQLiteException ex)
            {
                string s = ex.Message;
            }
    
            //
            return bOk;
        }
    

    `

    get items

    `

    public static List GetAll()
    {
    //
    List list= new List();

            try
            {
                list = db.Table<Item>().ToList();
            }
            catch (SQLiteException ex)
            {
                //
                string error = ex.Message;
            }
    
            //
            return list;
        }
    

    `

    hope this help

    First of all , Thanks alot

    we are talking about exist sqlite database file.

    do you have an example about it.

    again Thanks

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭✭

    see the getall method, this method open a connection and get the data from the database.
    If you don't know the structure of your db, download and install on your windows db browser sql lite and open the database with it ...

    a+

  • SampathTharangaSampathTharanga USMember ✭✭
    @ChristopheBERNARD I got it. Thank for your help.
  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭✭

    don't mention it you're welcome :)

Sign In or Register to comment.