YXTS122 2016-12-15 01:10 采纳率: 100%
浏览 1078
已采纳

安卓,创建数据库时崩溃了

DatabaseHelper.java

 package com.exampl.mysql;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

public class DatabaseHelper extends SQLiteOpenHelper{
    private Context mContext;
    public static final String CREATE_TABLE_SQL="create table dict(_id integer primary key autoincrement,word text,detail text);";

    public DatabaseHelper(Context context,String name,CursorFactory factory,int version)
    {
        super(context,name,factory,version);
        mContext=context;
    }

    public void onCreate(SQLiteDatabase db)
    {
        db.execSQL(CREATE_TABLE_SQL);
        Toast.makeText(mContext,"创建成功!",Toast.LENGTH_LONG).show();
    }

    public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)
    {

    }


}

MainActivity.java

 package com.exampl.mysql;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
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.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{
    private Button insert=null;
    private Button search=null;
    private EditText word=null;
    private EditText detail=null;
    private EditText key=null;
    DatabaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dbHelper=new DatabaseHelper(this,"myDict.db3",null,1);
        insert=(Button)findViewById(R.id.insert);
        insert.setOnClickListener(this);
        search=(Button)findViewById(R.id.search);
        search.setOnClickListener(this);
        word=(EditText)findViewById(R.id.word);
        detail=(EditText)findViewById(R.id.detail);
        key=(EditText)findViewById(R.id.key);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.insert:
            String wor=word.getText().toString().trim();
            String det=detail.getText().toString().trim();
            insertData(dbHelper.getReadableDatabase(),wor,det);
            Toast.makeText(this, "添加生词成功!", Toast.LENGTH_LONG).show();
            break;
        case R.id.search:
            String ke=key.getText().toString().trim();
            Cursor cursor=dbHelper.getReadableDatabase().rawQuery("select*from dict where word like? or details like?", new String[]{"%"+ke+"%","%"+ke+"%"});
            Bundle data=new Bundle();
            data.putSerializable("date", converCursorToList(cursor));
            Intent intent=new Intent(MainActivity.this,Result.class);
            intent.putExtras(data);
            startActivity(intent);
            break;
        default:break;
        }
    }

    protected ArrayList<Map<String,String>> converCursorToList(Cursor cursor)
    {
        ArrayList<Map<String,String>> result=new ArrayList<Map<String,String>>();
        while (cursor.moveToNext())
        {
            Map<String,String> map=new HashMap<String,String>();
            map.put("word", cursor.getString(1));
            map.put("detail", cursor.getString(2));
            result.add(map);
        }
        return result;
    }

    private void insertData(SQLiteDatabase db,String word,String detail)
    {
        db.execSQL("insert into dict values(null,?,?)"+new String[]{word,detail});
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        if (dbHelper!=null)
            dbHelper.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);
    }
}

Result.java

 package com.exampl.mysql;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Result extends Activity {
    private TextView jieguo=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.line);
        jieguo=(TextView)findViewById(R.id.jieguo);
        Bundle bundle=getIntent().getExtras();
        jieguo.setText(bundle.getString("date"));
    }

}

图片说明
图片说明
图片说明

  • 写回答

6条回答 默认 最新

  • stone_you 2016-12-15 10:09
    关注

    改成这样应该可以了
    db.execSQL("insert into dict (word, detail) values(?,?)", new String[]{word,detail});
    字符串这么拼是不对的,new String[]{word,detail}是一个对象,拼出来的不是sql语句

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里