需要帮助从android中的数据库中检索数据 [英] need help in retrieving data from database in android

查看:104
本文介绍了需要帮助从android中的数据库中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个Android初学者....编写一个非常简单的应用程序...它接收员工ID,名称并将其存储在数据库中......并在单击DISPLAY时显示详细信息。 />


当我点击保存按钮时没有错误..但是当我点击显示时,它显示不安全的应用程序停止了...请帮助...这里是我的代码



i am an android beginner....coding a very simple app...which takes in an employee id,name and stores it in a database...and shows the details when "DISPLAY" is clicked.

there is no error when i click the save button..but it shows "UNFORTUNATELY THE APP HAS STOPPED" when i click display..please help...here is my code

/* THIS IS MAINACTIVITY .JAVA */

package com.example.emp_db;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.database.Cursor;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;

public class MainActivity extends Activity {

	static SQLiteDatabase db= null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Myhelper mdh =new Myhelper(this,"employeedb",null,1) ;
		db=mdh.getWritableDatabase();
		
		Button save =(Button)findViewById(R.id.button1);
		Button disp=(Button)findViewById(R.id.button2);
		
		
		save.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
			EditText eid_us = (EditText)findViewById(R.id.edittext_id)	;
			EditText ename_us =(EditText)findViewById(R.id.editText_name) ;
			
			int eid = 22;//Integer.parseInt(eid_us.getText().toString());
			String ename ="madhuri";//ename_us.getText().toString();
			
			ContentValues cv =new ContentValues();
			cv.put("eno", eid);
			cv.put("ename", ename);
			
			db.insert("employee", null, cv);
			
			}
		});
		
		disp.setOnClickListener(new View.OnClickListener() {
		
			
			

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
			String[] col=new String[] {"eno","ename"};
			Cursor c= db.query("employee", col, null, null, null, null, null);
			String eno_db;
			 if(c.getString(1)!="") eno_db=c.getString(1); else
			  eno_db="it is null";
			 TextView t1=(TextView)findViewById(R.id.text_display);
				 t1.setText(eno_db);
			}
		});
	
    
	
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

*************************************************************
/*           THIS IS MYHELPER.JAVA           */
package com.example.emp_db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class Myhelper extends SQLiteOpenHelper {

	public Myhelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
		// TODO Auto-generated constructor stub
	}
	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		String str="create table employee(eno integer , ename text)";
		db.execSQL(str);
	}
	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
	}
}

************ ********************************************** *

* /这是ACTIVITY_MAIN.XML * /



******************************************************************************************
*/ THIS IS ACTIVITY_MAIN.XML */

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android">
    xmlns:tools="http://schemas.android.com/tools"
    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=".MainActivity" >

    <textview>
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_centerVertical="false"
        android:text="@string/emp_id"
        android:textSize="40sp" />

    <Button

        android:id="@+id/button2"

        style="?android:attr/buttonStyleSmall"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignRight="@+id/edittext_id"

        android:layout_alignTop="@+id/button1"

        android:text="@string/str_display" />

    <textview>
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/edittext_id"
        android:layout_marginLeft="61dp"
        android:layout_marginTop="26dp"
        android:text="@string/emp_name"
        android:textSize="30sp" />

    <edittext>
        android:id="@+id/editText_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView2"
        android:ems="10"
        android:inputType="textPersonName" >
        <requestfocus />
    </edittext>

    <edittext>
        android:id="@+id/edittext_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView1"
        android:ems="10"
        android:inputType="textPersonName" />

    <Button

        android:id="@+id/button1"

        style="?android:attr/buttonStyleSmall"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignRight="@+id/textView1"

        android:layout_below="@+id/editText_name"

        android:layout_marginTop="26dp"

        android:text="@string/str_save" />

    <textview>
        android:id="@+id/text_display"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText_name"
        android:layout_below="@+id/button1"
        android:layout_marginTop="55dp"
        android:textSize="40sp" />
</textview></edittext></textview></textview></relativelayout>



***************************************** **************************************************


*******************************************************************************************

推荐答案

检查此链接:



basic-databse-operations-in-sqlite.html [ ^ ]





查看上面链接中的文章,看看光标是如何使用的。
check this link:

basic-databse-operations-in-sqlite.html[^]


check the article in above link and see how cursor is using.


这篇关于需要帮助从android中的数据库中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆