Click here to Skip to main content
14,876,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I developing a small mobile app using xamarin forms in visual studio. I want to fill the local sqlite database with data from my api develop in php. When I pass the json data into listview it work fine but when I try to insert into the database it keep telling me to specify database path. Below is my code that i have tried so far. Please help me. Thanks in advance.

What I have tried:

Database in App.xaml.cs

static SQLiteDb db;

        public static SQLiteDb SQLiteDb
        {
            get
            {
                if (db == null)
                {
                    db = new SQLiteDb(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "ADIPCCU.db3"));
                }
                return db;
            }
        }



.....User Data Model.....

public class User
    {
        [PrimaryKey, AutoIncrement]
        public int id { get; set; }
        public string username { get; set; }
        public string password { get; set; }
        public string userrole { get; set; }
        public string userlocation { get; set; }
    }


.....This is where i want to populate the database....

public async void GetLocations()
    {
        var httpClient = new HttpClient();
        var response = await httpClient.GetStringAsync("http://192.168.100.145:8080/api.com/user/read.php");
        var users = JsonConvert.DeserializeObject<List<User>>(response);

        UsersListView.ItemsSource = users;

        SQLiteConnection conn = new SQLiteConnection(App.DatabaseLocation);
        conn.CreateTable<User>();
        conn.Insert(users);
        conn.Close();


    }


<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

//PDO is a extension which  defines a lightweight, consistent interface for accessing databases in PHP.  
$db=new PDO('mysql:dbname=tophigh;host=localhost;','root','12345');  
//here prepare the query for analyzing, prepared statements use less resources and thus run faster  
$row=$db->prepare('select * from user_t');  
  
$row->execute();//execute the query  
$json_data=array();//create the array  
foreach($row as $rec)//foreach loop  
{  
$json_array['id']=$rec['id'];  
    $json_array['username']=$rec['username'];  
    $json_array['password']=$rec['password'];  
    $json_array['userrole']=$rec['userrole']; 
    $json_array['userlocation']=$rec['userlocation'];
    $json_array['syncode']=$rec['syncode'];
//here pushing the values in to an array  
    array_push($json_data,$json_array);  
  
}  
  
//built in PHP function to encode the data in to JSON format  
echo json_encode($json_data);  
  
  
?>  
Posted
Comments
Mathiudi 14-Apr-21 19:37pm
   
Hellooooo
Mathiudi 15-Apr-21 12:25pm
   
No one want to help. Alright.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900