我通过观看教程视频开发了这个Android应用程序,其中他们共享了下载代码的链接。此应用程序在单击按钮时保存表单的值。单击时,它会显示插入值的消息,但我无法在sqlite中看到任何值,也无法检索任何值。
以下是该应用的完整代码: -
add.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="20dp" >
<EditText
android:id="@+id/editTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="hint_title" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editDuedate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="hint_duedate"
android:inputType="date" />
<EditText
android:id="@+id/editCourse"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="hint_course" />
<EditText
android:id="@+id/editNotes"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="notes"
android:inputType="textMultiLine" android:lines="6"/>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/addAssignment"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:onClick="addAssignment"
android:text="add" />
<Button
android:id="@+id/viewBtn"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="view" android:onClick="viewAssignments"/>
</LinearLayout>
</LinearLayout>
assignment_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/rowDate"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:padding="10dp"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:layout_alignParentRight="true"
/>
<TextView
android:id="@+id/row"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:layout_alignParentLeft="@+id/rowDate"
/>
</RelativeLayout>
main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="20dp" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="331dp"
android:layout_weight="0.44"
android:fillViewport="true" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0" >
</ListView>
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="add" />
</LinearLayout>
addassignment.java:
public class addassignment extends Activity {
DBAdapter db = new DBAdapter(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add);
}
public void addAssignment(View v)
{
Log.d("test", "adding");
//get data from form
EditText nameTxt = (EditText)findViewById(R.id.editTitle);
EditText dateTxt = (EditText)findViewById(R.id.editDuedate);
EditText courseTxt = (EditText)findViewById(R.id.editCourse);
EditText notesTxt = (EditText)findViewById(R.id.editNotes);
db.open();
long id = db.insertRecord(nameTxt.getText().toString(), dateTxt.getText().toString(), courseTxt.getText().toString(), notesTxt.getText().toString());
db.close();
nameTxt.setText("");
dateTxt.setText("");
courseTxt.setText("");
notesTxt.setText("");
Toast.makeText(addassignment.this,"Assignment Added", Toast.LENGTH_LONG).show();
}
public void viewAssignments(View v)
{
Intent i = new Intent(this, AssignmentTracker.class);
startActivity(i);
}
}
AssignmentTracker.java:
public class AssignmentTracker extends Activity {
/** Called when the activity is first created. */
//DBAdapter db = new DBAdapter(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button addBtn = (Button)findViewById(R.id.add);
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(AssignmentTracker.this, addassignment.class);
startActivity(i);
}
});
try {
String destPath = "/data/data/" + getPackageName() + "/databases/AssignmentDB";
File f = new File(destPath);
if (!f.exists()) {
CopyDB( getBaseContext().getAssets().open("mydb"),
new FileOutputStream(destPath));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//DBAdapter db = new DBAdapter(this);
//---add an assignment---
/*
db.open();
long id = db.insertRecord("Hello World", "2/18/2012", "DPR 224", "First Android Project");
id = db.insertRecord("Workbook Exercises", "3/1/2012", "MAT 100", "Do odd numbers");
db.close();
*/
//---get all Records---
/*
db.open();
Cursor c = db.getAllRecords();
if (c.moveToFirst())
{
do {
DisplayRecord(c);
} while (c.moveToNext());
}
db.close();
*/
/*
//---get a Record---
db.open();
Cursor c = db.getRecord(2);
if (c.moveToFirst())
DisplayRecord(c);
else
Toast.makeText(this, "No Assignments found", Toast.LENGTH_LONG).show();
db.close();
*/
//---update Record---
/*
db.open();
if (db.updateRecord(1, "Hello Android", "2/19/2012", "DPR 224", "First Android Project"))
Toast.makeText(this, "Update successful.", Toast.LENGTH_LONG).show();
else
Toast.makeText(this, "Update failed.", Toast.LENGTH_LONG).show();
db.close();
*/
/*
//---delete a Record---
db.open();
if (db.deleteRecord(1))
Toast.makeText(this, "Delete successful.", Toast.LENGTH_LONG).show();
else
Toast.makeText(this, "Delete failed.", Toast.LENGTH_LONG).show();
db.close();
*/
}
private class DBAdapter extends BaseAdapter {
private LayoutInflater mInflater;
//private ArrayList<>
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
return null;
}
}
public void CopyDB(InputStream inputStream, OutputStream outputStream)
throws IOException {
//---copy 1K bytes at a time---
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
}
public void DisplayRecord(Cursor c)
{
Toast.makeText(this,
"id: " + c.getString(0) + "\n" +
"Title: " + c.getString(1) + "\n" +
"Due Date: " + c.getString(2),
Toast.LENGTH_SHORT).show();
}
public void addAssignment(View view)
{
Intent i = new Intent("com.pinchtapzoom.addassignment");
startActivity(i);
Log.d("TAG", "Clicked");
}
}
DBAdapter.java:
public class DBAdapter {
public static final String KEY_ROWID = "id";
public static final String KEY_TITLE = "title";
public static final String KEY_DUEDATE = "duedate";
public static final String KEY_COURSE = "course";
public static final String KEY_NOTES = "notes";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "AssignmentsDB";
private static final String DATABASE_TABLE = "assignments";
private static final int DATABASE_VERSION = 3;
private static final String DATABASE_CREATE =
"create table if not exists assignments (id integer primary key autoincrement, "
+ "title VARCHAR not null, duedate date, course VARCHAR, notes VARCHAR );";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
try {
db.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
}
//---opens the database---
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
//---insert a record into the database---
public long insertRecord(String title, String duedate, String course, String notes)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_DUEDATE, duedate);
initialValues.put(KEY_COURSE, course);
initialValues.put(KEY_NOTES, notes);
return db.insert(DATABASE_TABLE, null, initialValues);
}
//---deletes a particular record---
public boolean deleteContact(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
//---retrieves all the records---
public Cursor getAllRecords()
{
return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE,
KEY_DUEDATE, KEY_COURSE, KEY_NOTES}, null, null, null, null, null);
}
//---retrieves a particular record---
public Cursor getRecord(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_TITLE, KEY_DUEDATE, KEY_COURSE, KEY_NOTES},
KEY_ROWID + "=" + rowId, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
//---updates a record---
public boolean updateRecord(long rowId, String title, String duedate, String course, String notes)
{
ContentValues args = new ContentValues();
args.put(KEY_TITLE, title);
args.put(KEY_DUEDATE, duedate);
args.put(KEY_COURSE, course);
args.put(KEY_NOTES, notes);
return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
}
我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pallavi.sqlitecon"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:label="@string/app_name" >
<activity
android:name=".AssignmentTracker"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".addassignment"></activity>
</application>
</manifest>
答案 0 :(得分:1)
使用sqlite
数据库浏览器检查您的表是否包含值
http://sqlitebrowser.org/
答案 1 :(得分:0)
在DatabaseHelper类中使用add below函数并在插入后调用它或者想要查看数据库
public static void DbBackup() {
File file = new File(Environment.getExternalStorageDirectory(),DATABASE_NAME);
if (file.exists())
file.delete();
try {
File dbFile = new File(DB_PATH + DB_NAME);
FileInputStream in = new FileInputStream(dbFile);
OutputStream out = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len;
try {
while ((len = in.read(buf)) > 0)
out.write(buf, 0, len);
} catch (IOException e) {
e.printStackTrace();
}
in.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
{{1} }是数据库文件的路径 当您调用此函数时,它会将数据库文件从数据库复制到SdCard,现在将此数据库文件传输到您的计算机并使用任何SQLite浏览器打开它以查看数据库的内容。
答案 2 :(得分:0)
您可以使用模拟器的DDMS访问此文件夹。
您可以在Eclipse中查看表结构和数据。以下是步骤
1.为Eclipse安装SqliteManagerPlugin。
2.从此处下载* .jar文件
3.将* .jar文件放入eclipse / dropins /
文件夹中4.Restart eclipse
5.在eclipse的右上角,单击DDMS图标
6.在左侧面板中选择适当的模拟器
7.在主面板的File Explorer选项卡中,转到/ data / data / [app package] / databases
8.在DDMS图标下,当您选择数据库时,应该会有一个新的数据库蓝色图标亮起。 单击它,您将看到一个Questoid Sqlite Manager选项卡打开以查看您的数据。
下载此Questoid SqLiteBrowser:http://www.java2s.com/Code/JarDownload/com.questoid/com.questoid.sqlitebrowser_1.2.0.jar.zip
解压缩并将其放入eclipse / dropins
了解更多信息see here
之后,您可以看到数据库中的数据是否已插入?
答案 3 :(得分:0)
要了解您在android studio中创建的sqlite数据库,您需要按照简单的步骤进行操作:
1.运行您的申请
2.转到工具---&gt; Android ----&gt;设备监视器
3.在左侧面板中找到您的应用程序名称
4.然后单击“文件资源管理器”选项卡
5.选择数据文件夹
6.再次选择数据文件夹,找到您的应用程序或模块名称
7.单击您的数据库名称
8.在右上角的窗口中,您可以选择从设备中提取文件。
9.单击它并将其保存在PC上
有关更多信息,请点击此链接。 http://www.c-sharpcorner.com/UploadFile/e14021/know-where-database-is-stored-in-android-studio/
要查看db文件中的数据,您需要下载sqlite浏览器或在任何浏览器中添加相同的插件,这样您就需要在浏览器中打开文件并查看数据。
下载浏览器