· 9 years ago · Sep 14, 2017, 01:16 PM
1Java:
2import java.util.ArrayList;
3
4import android.app.Activity;
5import android.database.Cursor;
6import android.os.Bundle;
7import android.view.View;
8import android.view.View.OnClickListener;
9import android.widget.Button;
10import android.widget.EditText;
11import android.widget.Toast;
12
13public class DatabaseActivity extends Activity
14{
15 ArrayList<String> results = new ArrayList<String>();
16
17 private EditText etUsername;
18 private Button btnLogin;
19 DBAdapter db = new DBAdapter(this);
20
21 @Override
22
23 public void onCreate(Bundle savedInstanceState) {
24 super.onCreate(savedInstanceState);
25 setContentView(R.layout.main);
26
27 etUsername = (EditText)findViewById(R.id.usernametxt);
28 btnLogin = (Button)findViewById(R.id.btnLogin);
29
30 db.open();
31 db.insertTitle("mani","mani");
32 db.insertTitle("sudhakar","sudhakar");
33 db.close();
34
35
36 btnLogin.setOnClickListener(new OnClickListener() {
37 @Override
38 public void onClick(View v) {
39 String username = etUsername.getText().toString();
40
41 db.open();
42
43 Cursor c = db.getTitles(username);
44 if (c.moveToFirst()) {
45 do {
46 String firstName = c.getString(1);
47 String lastname = c.getString(2);
48 results.add("Firstname" + firstName + "Lastname: " + lastname);
49 }while (c.moveToNext());
50 }
51
52 Toast.makeText(DatabaseActivity.this, (CharSequence) results,Toast.LENGTH_SHORT).show();
53
54 db.close();
55
56 }
57 });
58 }
59
60
61}
62
63
64DBAdapter:
65
66package com.database.demo.user;
67
68
69import android.content.ContentValues;
70import android.content.Context;
71import android.database.Cursor;
72import android.database.SQLException;
73import android.database.sqlite.SQLiteDatabase;
74import android.database.sqlite.SQLiteOpenHelper;
75import android.util.Log;
76
77
78public class DBAdapter
79{
80 public static final String KEY_ROWID = "_id";
81 public static final String KEY_USER = "user";
82 public static final String KEY_PASS = "pass";
83 private static final String TAG = "DBAdapter";
84
85 private static final String DATABASE_NAME = "login";
86 private static final String DATABASE_TABLE = "userpass";
87 private static final int DATABASE_VERSION = 1;
88
89 private static final String DATABASE_CREATE =
90 "create table userpass (_id integer primary key autoincrement, "
91 + "user text not null, pass text not null); ";
92
93
94 private final Context context;
95
96 private DatabaseHelper DBHelper;
97 private SQLiteDatabase db;
98
99 public DBAdapter(Context ctx)
100 {
101 this.context = ctx;
102 DBHelper = new DatabaseHelper(context);
103 }
104
105 private static class DatabaseHelper extends SQLiteOpenHelper
106 {
107 DatabaseHelper(Context context)
108 {
109 super(context, DATABASE_NAME, null, DATABASE_VERSION);
110 }
111
112
113
114 @Override
115 public void onCreate(SQLiteDatabase db)
116 {
117 db.execSQL(DATABASE_CREATE);
118 }
119
120
121
122 @Override
123 public void onUpgrade(SQLiteDatabase db, int oldVersion,
124 int newVersion)
125 {
126 Log.w(TAG, "Upgrading database from version " + oldVersion
127 + " to "
128 + newVersion + ", which will destroy all old data");
129 db.execSQL("DROP TABLE IF EXISTS titles");
130 onCreate(db);
131 }
132
133 }
134 //---opens the database---
135 public DBAdapter open() throws SQLException
136 {
137 db = DBHelper.getWritableDatabase();
138 return this;
139 }
140
141 //---closes the database---
142 public void close()
143 {
144 DBHelper.close();
145 }
146
147 //---insert a title into the database---
148 public long insertTitle(String user, String pass)
149 {
150 ContentValues initialValues = new ContentValues();
151 initialValues.put(KEY_USER, user);
152 initialValues.put(KEY_PASS, pass);
153 return db.insert(DATABASE_TABLE, null, initialValues);
154 }
155
156 //---deletes a particular title---
157 public boolean deleteTitle(long rowId)
158 {
159 return db.delete(DATABASE_TABLE, KEY_ROWID +
160 "=" + rowId, null) > 0;
161 }
162
163 //---retrieves all the titles---
164 public Cursor getAllTitles()
165 {
166 return db.query(DATABASE_TABLE, new String[] {
167 KEY_ROWID,
168 KEY_USER,
169 KEY_PASS},
170 null,
171 null,
172 null,
173 null,
174 null);
175 }
176
177 public Cursor getTitles(String searchValue)
178 {
179 return db.rawQuery( "SELECT * FROM "+DATABASE_TABLE +" WHERE user LIKE '%"+searchValue+"%'", null);
180
181 }
182
183 //---retrieves a particular title---
184 public Cursor getTitle(long rowId) throws SQLException
185 {
186 Cursor mCursor =
187 db.query(true, DATABASE_TABLE, new String[] {
188 KEY_ROWID,
189 KEY_USER,
190 KEY_PASS,
191
192 },
193 KEY_ROWID + "=" + rowId,
194 null,
195 null,
196 null,
197 null,
198 null);
199 if (mCursor != null) {
200 mCursor.moveToFirst();
201 }
202 return mCursor;
203 }
204
205
206 //---updates a title---
207 public boolean updateTitle(long rowId, String isbn,
208 String title, String publisher)
209 {
210 ContentValues args = new ContentValues();
211 args.put(KEY_USER, isbn);
212 args.put(KEY_PASS, title);
213
214 return db.update(DATABASE_TABLE, args,
215 KEY_ROWID + "=" + rowId, null) > 0;
216 }
217
218 }