Error while working with SQLite

  • Replies:1
StOrm RiDeR
  • Forum posts: 1

Aug 15, 2016, 12:28:26 AM via Website

Hello Guys,

I'm new to Android Development and I'm working with SQLite for my application.

When I run the app in the VM or in my handheld device when I'm inserting data into the table i'm getting a loop of an error/warning stating like this

08-15 03:43:10.180 20006-20006/com.example.miracle.sqlexample D/dalvikvm: GC_FOR_ALLOC freed 5520K, 49% free 6702K/12928K, paused 13ms, total 13ms
08-15 03:43:10.230 20006-20006/com.example.miracle.sqlexample D/dalvikvm: GC_FOR_ALLOC freed 5492K, 49% free 6697K/12928K, paused 13ms, total 13ms
08-15 03:43:10.270 20006-20006/com.example.miracle.sqlexample D/dalvikvm: GC_FOR_ALLOC freed 5452K, 49% free 6708K/12928K, paused 14ms, total 14ms
<<
08-15 03:43:10.310 20006-20006/com.example.miracle.sqlexample D/dalvikvm: GC_FOR_ALLOC freed 5454K, 49% free 6710K/12928K, paused 12ms, total 12ms
08-15 03:43:10.340 20006-20006/com.example.miracle.sqlexample D/dalvikvm: GC_FOR_ALLOC freed 5466K, 49% free 6700K/12928K, paused 11ms, total 11ms
08-15 03:43:10.370 20006-20006/com.example.miracle.sqlexample D/dalvikvm: GC_FOR_ALLOC freed 5414K, 49% free 6714K/12928K, paused 12ms, total 12ms
08-15 03:43:10.410 20006-20006/com.example.miracle.sqlexample D/dalvikvm: GC_FOR_ALLOC freed 5401K, 48% free 6723K/12928K, paused 12ms, total 12ms
08-15 03:43:10.440 20006-20006/com.example.miracle.sqlexample D/dalvikvm: GC_FOR_ALLOC freed 5417K, 49% free 6703K/12928K, paused 12ms, total 12ms
08-15 03:43:10.471 20006-20006/com.example.miracle.sqlexample D/dalvikvm: GC_FOR_ALLOC freed 5422K, 49% free 6704K/12928K, paused 12ms, total 12ms
08-15 03:43:10.511 20006-20006/com.example.miracle.sqlexample D/dalvikvm: GC_FOR_ALLOC freed 5421K, 49% free 6705K/12928K, paused 13ms, total 13ms

and this is getting repeated untill I quit the application.
below is the source code that I'm using for.

MainActivity.java

package com.example.miracle.sqlexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

EditText input;
Button ins,del;
TextView result;
DBHelper db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    input = (EditText)findViewById(R.id.insertText);
    ins = (Button)findViewById(R.id.insertBtn);
    del = (Button)findViewById(R.id.deleteBtn);
    result = (TextView)findViewById(R.id.resultView);
    db = new DBHelper(this,null,null,1);
}

public void insertData(View v)
{
    Boolean insert = false ;
    insert = db.insertEmp(input.getText().toString());

    if(insert == true)
    {
        Toast.makeText(this,"Employee Name Inserted",Toast.LENGTH_LONG).show();
    }
    printDb();
}

public void deleteData(View v)
{
  Boolean delete = db.delEmp(input.getText().toString());

    if(delete == true)
    {
        Toast.makeText(this,"Employee Name Deleted",Toast.LENGTH_LONG).show();
    }
    printDb();
}

public void printDb()
{
    String resultDB = db.printDB();
    result.setText(resultDB);
}

}

DBHelper.java

package com.example.miracle.sqlexample;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
* Created by Miracle on 15-Aug-16.
*/
public class DBHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "Emp.db";
public static final String Employee_TABLE_NAME = "Employee";
public static final String Employee_COLUMN_EMPNAME = "EmployeeName";

public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase sqlDB) {

    sqlDB.execSQL("create table "+Employee_TABLE_NAME+" ("+Employee_COLUMN_EMPNAME+" text)");
}

@Override
public void onUpgrade(SQLiteDatabase sqlDB, int i, int i1) {

    sqlDB.execSQL("drop table if exists "+Employee_TABLE_NAME);
    onCreate(sqlDB);
}

//Insert EmpName

public boolean insertEmp(String EmpName)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues cValues = new ContentValues();
    cValues.put(Employee_COLUMN_EMPNAME,EmpName);
    db.insert(Employee_TABLE_NAME,null,cValues);
    db.close();
    return true;
}

public boolean delEmp(String EmpName)
{
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(Employee_TABLE_NAME,"EmployeeName = ?",new String[]{EmpName});
    return true;
}

public String printDB()
{
    String dbString = null;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c = db.rawQuery("select * from "+Employee_TABLE_NAME,null);

    c.moveToFirst();

    while(!c.isAfterLast())
    {
        if(c.getString(c.getColumnIndex("EmployeeName"))!=null)
        {
            dbString += c.getString(c.getColumnIndex("EmployeeName"));
            dbString += "\n";
        }
    }

    db.close();
    return dbString;
}

}

*activity_main.xml*


android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.miracle.sqlexample.MainActivity">

<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/insertText"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Insert"
    android:id="@+id/insertBtn"
    android:layout_below="@+id/insertText"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="42dp"
    android:onClick="insertData" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Delete"
    android:id="@+id/deleteBtn"
    android:layout_alignTop="@+id/insertBtn"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:onClick="deleteData" />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="250dp"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:id="@+id/resultView"
    android:layout_below="@+id/insertBtn"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="40dp"
    android:gravity="center_vertical|center_horizontal" />

please provide me with your suggestions.

Thanks,
Abhinay

— modified on Aug 15, 2016, 12:47:37 AM

Reply
Ashish Tripathi
  • Forum posts: 211

Aug 17, 2016, 7:51:40 AM via Website

As per by your logs , first of all it is not error just saying the space using by Garbage collector. Second you are not closing your database any where. What the scenario is if you have done work with db close it and again open if required.
while(!c.isAfterLast())
{
if(c.getString(c.getColumnIndex("EmployeeName"))!=null)
{
dbString += c.getString(c.getColumnIndex("EmployeeName"));
dbString += "\n";
}
}

are you getting the next element??

Reply