AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Android studio sqlite database ex3/18/2024 ![]() The download is categorized by versions, so browse a directory for a specific version you want: 3.5.9, 3.6.16, 3.7.2, etc. Download SQLite JDBC driverYou can download the latest version of JDBC driver for SQLite here. Although there is no official JDBC driver library from there is one provided by – an XML Database Management System project. Import is a simple, small, fast, reliable, server-less, zero-configuration and no-installation SQL database library which is running in-process with the client application. Although there are no syntax for run time errors when I run the code, it does not give me any output neither does it shows any message as specified in the code. I had used the same code that you provided for sqlite database with a few modifications but unfortunately, it did not work as I expected. I'm new to android programming with only a passable background on C,C++ programming, etc. The following user-defined function is used to display message to the user: ShowMessage( " Student Management Application", " Developed By Azim") ShowMessage( " Student Details", buffer.toString()) ![]() ShowMessage( " Error", " No records found") īuffer.append( " Rollno: "+c.getString( 0)+ " \n") īuffer.append( " Name: "+c.getString( 1)+ " \n") īuffer.append( " Marks: "+c.getString( 2)+ " \n\n") Checking if no records found if(c.getCount()=0) ![]() Viewing all records if(view=btnViewAll)Ĭursor c=db.rawQuery( " SELECT * FROM student", null) ShowMessage( " Success", " Record Modified") " ' WHERE rollno='"+editRollno.getText()+ " '") Modifying a record if(view=btnModify)ĭb.execSQL( " UPDATE student SET name='"+editName.getText()+ " ',marks='"+editMarks.getText()+ ShowMessage( " Error", " Invalid Rollno") ShowMessage( " Success", " Record Deleted") ShowMessage( " Error", " Please enter Rollno") Ĭursor c=db.rawQuery( " SELECT * FROM student WHERE rollno='"+editRollno.getText()+ " '", null) ĭb.execSQL( " DELETE FROM student WHERE rollno='"+editRollno.getText()+ " '") Checking empty roll number if(editRollno.getText().toString().trim().length()=0) ShowMessage( " Success", " Record added") ShowMessage( " Error", " Please enter all values") ĭb.execSQL( " INSERT INTO student VALUES('"+editRollno.getText()+ " ','"+editName.getText()+ Checking empty fields if(editRollno.getText().toString().trim().length()=0||ĮditName.getText().toString().trim().length()=0||ĮditMarks.getText().toString().trim().length()=0) The following code uses the db.execSQL() function to insert a student record in the student table. In the onClick() event handler, we can write the code required to add, delete, modify and view records. Public void onCreate(Bundle savedInstanceState)ĮditRollno=(EditText)findViewById(R.id.editRollno) ĮditName=(EditText)findViewById(R.id.editName) ĮditMarks=(EditText)findViewById(R.id.editMarks) ītnAdd=(Button)findViewById(R.id.btnAdd) ītnDelete=(Button)findViewById(R.id.btnDelete) ītnModify=(Button)findViewById(R.id.btnModify) ītnView=(Button)findViewById(R.id.btnView) ītnViewAll=(Button)findViewById(R.id.btnViewAll) ītnShowInfo=(Button)findViewById(R.id.btnShowInfo) ĭb=openOrCreateDatabase( " StudentDB", Context.MODE_PRIVATE, null) ĭb.execSQL( " CREATE TABLE IF NOT EXISTS student(rollno VARCHAR,name VARCHAR,marks VARCHAR) ") The following code shows how to create an SQLite database and a table in the database. The SQLiteDatabase class from the package and the Cursor class from the android.database package provide all the functionality required for performing Data Manipulation Language (DML) and query operations on an SQLite table. For simplicity, I have created all fields of VARCHAR data type, which is a variable length character string. The application accepts a student's roll number, name and marks and adds these details to a student table. The example application which I have created is a simple Student Management System, which allows a user to add, delete, modify and view student details. The example application shows how to perform basic DML and query operations on an SQLite table in Android. This article assumes that the user has a working knowledge of Android and basic SQL commands. Also most of the examples assume a deep knowledge of Android and SQL. Most of the articles and demos which I have seen on the net were not very simple for a layman to understand. In this article, I have attempted to demonstrate the use of SQLite database in Android in the simplest manner possible.
0 Comments
Read More
Leave a Reply. |