sqlite + mysql + json.net

ShumzShumz INMember ✭✭

i have php code :

$sql = "SELECT * FROM UserMaster WHERE mobile_no = '$mNumber' AND password = '$mPassword'";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc()) {
$Details = array();
$detail = array("userid" => $row['userid'],
"username" => $row['username'],
"mobile_no" => $row['mobile_no'],
"password" => $row['password'],
"email" => $row['email'],
"role" => $row['role'],
"datetime" => $row['datetime'],
);
array_push($Details, $detail);
echo json_encode($Details );
}

whose result id :

[{"userid":"168","username":"shuqqmezaqq","mobile_no":"111111112","password":"33329999","email":"","role":"patient","datetime":"2016-09-08 11:36:25"}]

and his output is:

public class RootObject
{
public string userid { get; set; }
public string username { get; set; }
public string mobile_no { get; set; }
public string password { get; set; }
public string email { get; set; }
public string role { get; set; }
public string datetime { get; set; }
}

now i want to store it on sqlite usint sqlite.net and json.net and also MYsql plugin

i am using C# coding in activity page.

private void Client_UploadValuesCompleted(object sender, UploadValuesCompletedEventArgs e)
{
string json = Encoding.UTF8.GetString(e.Result);
int userid = 0;
int.TryParse(id, out userid);
try
{
string dpPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "Masteruser.db3"); //Create New Database
var db = new SQLiteConnection(dpPath);
db.CreateTable();
Login tbl = new Login();
tbl.userid = userid;
tbl.username = mtxtFirstName.Text;
tbl.mobile_no = mtxtEmial.Text;
tbl.password = mtxtPassword.Text;
db.Insert(tbl);
Toast.MakeText(this, "Record Added Successfully...,", ToastLength.Short).Show();
var activity2 = new Intent(this, typeof(HomeActivity));
activity2.PutExtra("userid", tbl.userid.ToString());
activity2.PutExtra("mobile_no", tbl.mobile_no.ToString());
StartActivity(activity2);

        Toast.MakeText(this, tbl.userid.ToString(), ToastLength.Short).Show();
        Toast.MakeText(this, tbl.mobile_no.ToString(), ToastLength.Short).Show();

    }
    catch (Exception ex)
    {
        Toast.MakeText(this, ex.ToString(), ToastLength.Short).Show();
    }

do u have any idea what should i make change in sqlite coding so that sqlite cable able to entr jason code

where my sqlite table is :

public class Login
{
[PrimaryKey]
public int userid { get; set; }

    [MaxLength(20)]
    public string username { get; set; }

    [MaxLength(20)]
    public string mobile_no { get; set; }

    [MaxLength(20)]
    public string password { get; set; }

    [MaxLength(20)]
    public string role { get; set; }

    [MaxLength(50)]
    public string datetime { get; set; }

}

Posts

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭

    Hello,

    normally the solution will be

    '
    var yourObject = JsonNet..Deserialize(YourJSonString);
    '

    after just insert into yourObject to sqlLite

    '
    db.Insert(yourObject)
    '

    hope this help

  • ShumzShumz INMember ✭✭

    i already did it but it doesn't work actually i want code in c# for retrieving array from php file and insert that data of array in my sqlite table that is on local machine. actually i want to retrieve data from MySQL server and add this ti sqlite table which is on local machine (mobile phone ) and thus i want only c# code of retrieving json array from php file and insert detail of table to sqlite table.

  • ShumzShumz INMember ✭✭

    string json = Encoding.UTF8.GetString(e.Result);
    Login_json p1 = JsonConvert.DeserializeObject(json);

       // int userid = 0;
       //int.TryParse(json, out userid);
        try
        {
            string dpPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "Masteruser.db3"); //Create New Database  
            var db = new SQLiteConnection(dpPath);
            db.CreateTable<Login_json>();
            Login_json tbl = new Login_json();
            //tbl.userid = userid;
            //tbl.username = mtxtFirstName.Text;
            //tbl.mobile_no = mtxtEmial.Text;
           // tbl.password = mtxtPassword.Text;
            db.Insert(p1);
            Toast.MakeText(this, "Record Added Successfully...,", ToastLength.Short).Show();
            //var activity2 = new Intent(this, typeof(HomeActivity));
            //activity2.PutExtra("userid", db.tbl.userid.ToString());
            //activity2.PutExtra("mobile_no", tbl.mobile_no.ToString());
            //StartActivity(activity2);
    
            //Toast.MakeText(this, tbl.userid.ToString(), ToastLength.Short).Show();
            //Toast.MakeText(this, tbl.mobile_no.ToString(), ToastLength.Short).Show();
    
        }
        catch (Exception ex)
        {
            Toast.MakeText(this, ex.ToString(), ToastLength.Short).Show();
        }
    

    i used this code but it give me an error:
    Newtonsoft.Json.JsonReaderException: Additional text encountered after finished reading JSON content: {. Path '', line 1, position 154.

    i dont have any idea why? please help me.

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭

    for me that's work fine

    string strJson ={'userid':168,'username':'shuqqmezaqq','mobile_no':111111112,'password':33329999,'email':'','role':'patient'}";
    var rootObject = Newtonsoft.Json.JsonConvert.DeserializeObject(strJson);

    public class RootObject
    {
        public string userid { get; set; }
        public string username { get; set; }
        public string mobile_no { get; set; }
        public string password { get; set; }
        public string email { get; set; }
        public string role { get; set; }
    }
    

    i can arrive to have your object => RootObjext i think there is a problem when you receive your json for me but i'm not an expert the json isn't well formatted !

    i've delete datetime properties because i have an error if i added

    hope this help

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭

    var rootObject = Newtonsoft.Json.JsonConvert.DeserializeObject < RootObject > (strJson);

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭

    if you want to retrieve a collection of object

    string jsonStringCollection = "[{'Id': 2, 'Name':' NAME 1'},{'Id': 2, 'Name':' NAME 2'},{'Id': 3, 'Name':' NAME 3'}]";
    var collection = Newtonsoft.Json.JsonConvert.DeserializeObject<System.Collections.Generic.List>(jsonStringCollection);

    public class IdName
    {
    public int Id { get; set; }
    public string Name { get; set; }
    }

  • ShumzShumz INMember ✭✭

    thank you so much for this help.it help me in some extent and now i feel that json error is gone and my application start debugging but i deploy application on android and try ti insert data into database it give me SQLite error. can u help me in this as still application is not working well.

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭

    @Shumz
    try to make a simple app with sql lite with for example just a little class an Id and a Name

    it's difficult to identity the error with your print screen

    if i'm shumz :) i've create a new app create a new class Person for example add some properties and after create a manager for sqllite with Insert, Update, Delete and so on ...

    when this little project will be work perfectly you can pass to a more complex data models ...

    hope this help

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭


    with these videos you can spend a excellent wkend :)

  • ShumzShumz INMember ✭✭

    thankyou so much

Sign In or Register to comment.