YXTS122 2016-12-17 23:07 采纳率: 100%
浏览 2206
已采纳

只能添加,不能更新,删除,查询数据,怎么改?

MainActivity.java

 package com.example.sqlite;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{
    private static final String TAG="Add"; 
    private static final ListAdapter ListAdapter = null; 
    private EditText ecode,ename,ebirth; 
    private Button badd,bdel,bupdate,bsele; 
    private SQLiteDatabase db=null; 
    private TextView tedatashow; 
    private ListView datashow;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ecode=(EditText)findViewById(R.id.ecode); 
        ename=(EditText)findViewById(R.id.ename); 
        ebirth=(EditText)findViewById(R.id.ebirth); 
        badd=(Button)findViewById(R.id.badd); 
        bdel=(Button)findViewById(R.id.bdel); 
        bupdate=(Button)findViewById(R.id.bupdate); 
        bsele=(Button)findViewById(R.id.bsele); 
        tedatashow=(TextView)findViewById(R.id.tedatashow); 
        datashow=(ListView)findViewById(R.id.datashow); 
        badd.setOnClickListener((android.view.View.OnClickListener) this); 
        bdel.setOnClickListener((android.view.View.OnClickListener) this); 
        bsele.setOnClickListener((android.view.View.OnClickListener) this); 
        bupdate.setOnClickListener((android.view.View.OnClickListener) this);
    }

    public void onClick(View v) 
    { 
        // TODO Auto-generated method stub 
        MyDBHelper helper=new MyDBHelper(this); 
        db=helper.getWritableDatabase(); 
        if(v==badd)
        {
            if(ecode.getText().toString().trim().length()!=0 && ename.getText().toString().trim().length()!=0 && ebirth.getText().toString().trim().length()!=0)
            {
                try
                {
                    String sql="INSERT INTO user(ecode,ename,ebirth)" +"VALUES('"+ecode.getText()+"','" +ename.getText()+"','" +ebirth.getText()+"')"; 
                    db.execSQL(sql); 
                    Toast.makeText(this, "添加成功!", Toast.LENGTH_LONG).show(); 
                    ecode.setText(""); 
                    ename.setText(""); 
                    ebirth.setText(""); 
                }
                catch(Exception e)
                {
                    Toast.makeText(this, "出错了!"+ e.getMessage(),Toast.LENGTH_LONG).show(); 
                } 
            }
            else
                Toast.makeText(this, "学号和姓名出生日期不能为空!", Toast.LENGTH_LONG).show(); 
        } 
        if(v==bdel)
        {
            if(ecode.getText().toString().trim().length()!=0)
            {
                try
                {
                    String sql="delete from user where ecode='"+ecode.getText()+"'"; 
                    db.execSQL(sql); 
                    Toast.makeText(this, "成功删除!", Toast.LENGTH_LONG).show(); 
                    ecode.setText(""); 
                }
                catch(Exception e)
                {
                    Toast.makeText(this, "出错了!", Toast.LENGTH_LONG).show(); 
                }
            }
        }
                if(v==bupdate)
                {
                    if(ecode.getText().toString().trim().length()!=0 && ename.getText().toString().trim().length()!=0 && ebirth.getText().toString().trim().length()!=0)
                    {
                        try
                        {
                            String sql="update user set ecode='"+ecode.getText() +"',ename='"+ename.getText()+"',birthday='" +ebirth.getText()+"'where code='" +ecode.getText()+"''"; 
                            db.execSQL(sql); 
                            Toast.makeText(this, "成功更新!", Toast.LENGTH_LONG).show(); 
                            ecode.setText(""); 
                            ename.setText(""); 
                            ebirth.setText(""); 
                        }
                        catch(Exception e)
                        {
                            Toast.makeText(this, "出错了!"+e.getMessage(),Toast.LENGTH_LONG).show(); 
                        } 
                    }
                    else
                        Toast.makeText(this, "学号姓名出生日期不能为空!", Toast.LENGTH_LONG).show(); 
                }
                if(v==bsele)
                {
                    if(ecode.getText().toString().trim().length()!=0)
                    {
                    try
                    {
                        List all=new ArrayList(); 
                        String sql="select * from user where ecode =? or ename =? or ebirth =? "; 
                        Cursor result=this.db.rawQuery(sql, null); 
                        for(result.moveToFirst();!result.isAfterLast();result.moveToNext())
                        {
                            all.add("["+result.getString(0)+"]"+""+result.getString(1)+","+result.getString(2)); 
                        }
                        datashow.setAdapter(ListAdapter);
                        new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1,all); 
                    }
                    catch(Exception f)
                    {
                        Toast.makeText(this, "显示不了", Toast.LENGTH_LONG).show(); 
                    }
                    }
                }
                db.close(); 
    }

    @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;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

MyDBHelper.java

 package com.example.sqlite;

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

public class MyDBHelper extends SQLiteOpenHelper{
    public MyDBHelper(Context context)
    {
        super(context,"mvdb.db",null,1);
    }

    public void onCreate(SQLiteDatabase db)
    {
        String sql="create table user(ecode text,ename text,ebirth text);";
        db.execSQL(sql);
    }

    public void onUpgrade(SQLiteDatabase db,int arg1,int arg2)
    {
        String sql="create table user(ecode text,ename text,ebirth text);";
        db.execSQL(sql);
        this.onCreate(db);
    }

}

  • 写回答

2条回答 默认 最新

  • dsxjinzhiqiang 2016-12-18 10:47
    关注

    如果需要更新数据库,需要每次将版本升高,你的版本始终都是一,还有,建议你将数据库的操作封装成工具类,这样清晰

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示