新人!安卓sqlite数据库报错
新人!安卓sqlite数据库报错
发布时间:2018-02-01 来源:查字典编辑
摘要:E/AndroidRuntime:FATALEXCEPTION:mainProcess:com.example.dell.kaoqin,PI...

E/AndroidRuntime: FATAL EXCEPTION: main

                  Process: com.example.dell.kaoqin, PID: 3408

                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dell.kaoqin/com.example.dell.kaoqin.Main2Activity}: android.database.sqlite.SQLiteException: no such column: id (code 1): , while compiling: select * from kaoqin where id=?

                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)

                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)

                      at android.app.ActivityThread.-wrap12(ActivityThread.java)

                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)

                      at android.os.Handler.dispatchMessage(Handler.java:102)

                      at android.os.Looper.loop(Looper.java:154)

                      at android.app.ActivityThread.main(ActivityThread.java:6077)

                      at java.lang.reflect.Method.invoke(Native Method)

                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)

                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

                   Caused by: android.database.sqlite.SQLiteException: no such column: id (code 1): , while compiling: select * from kaoqin where id=?

                      at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)

                      at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)

                      at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)

                      at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)

                      at android.database.sqlite.SQLiteProgram.< init >(SQLiteProgram.java:58)

                      at android.database.sqlite.SQLiteQuery.< init >(SQLiteQuery.java:37)

                      at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)

                      at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1318)

                      at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1257)

                      at com.example.dell.kaoqin.Dao.queryday(Dao.java:87)

                      at com.example.dell.kaoqin.Main2Activity.onCreate(Main2Activity.java:30)

                      at android.app.Activity.performCreate(Activity.java:6664)

                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)

                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)

                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 

                      at android.app.ActivityThread.-wrap12(ActivityThread.java) 

                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 

                      at android.os.Handler.dispatchMessage(Handler.java:102) 

                      at android.os.Looper.loop(Looper.java:154) 

                      at android.app.ActivityThread.main(ActivityThread.java:6077) 

                      at java.lang.reflect.Method.invoke(Native Method) 

                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 

                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

代码如下

package com.example.dell.kaoqin;

import android.app.AlertDialog;

import android.content.Intent;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private EditText id,password;

    private Button btns,btnt;

    private Dao dao;

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        dao=new Dao(MainActivity.this);

        init();

        btns.setOnClickListener(studentlistener);

        btnt.setOnClickListener(teacherlistener);

    }

    public void init(){

        id= (EditText) findViewById(R.id.id2);

        password= (EditText) findViewById(R.id.password2);

        btns= (Button) findViewById(R.id.btn_student);

        btnt= (Button) findViewById(R.id.btn_teacher);

    }

        View.OnClickListener studentlistener = new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                if (dao.isboolean(Integer.parseInt(id.getText().toString()),password.getText().toString())){

                    Intent intent=new Intent(MainActivity.this,Main2Activity.class);

                    Bundle bundle=new Bundle();

                    bundle.putString("id",id.getText().toString());

                    intent.putExtras(bundle);

                    startActivityForResult(intent,1);

                    id.setText("");

                    password.setText("");

                    Toast.makeText(MainActivity.this,"登录成功!",Toast.LENGTH_LONG).show();

                }else if (id.getText().toString()==null||password.getText().toString()==null){

                    Toast.makeText(MainActivity.this,"错误!",Toast.LENGTH_LONG).show();

                }else

                    Toast.makeText(MainActivity.this,"账号或密码错误!",Toast.LENGTH_LONG).show();

            }

        };

        View.OnClickListener teacherlistener = new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                if (id.getText().toString().equals("aidi")&&password.getText().toString().equals("123")) {

                    Intent intent = new Intent(MainActivity.this, Main22Activity.class);

                    startActivity(intent);

                    Toast.makeText(MainActivity.this,"登录成功!",Toast.LENGTH_LONG).show();

                }else

                    Toast.makeText(MainActivity.this,"账号或密码错误!",Toast.LENGTH_LONG).show();

            }

        };

}

import java.util.ArrayList;

import java.util.List;

/**

 * Created by dell on 2017/6/12.

 */

public class Dao {

    private help helper;

    public Dao(Context context) {

        helper=new help(context);

    }

    //老师插入学生考勤情况

    public void insertteacher(student student){

        SQLiteDatabase db=helper.getReadableDatabase();

        ContentValues values=new ContentValues();

        values.put("id",student.getId());

        values.put("day",student.getKecheng());

        values.put("kecheng",student.getKecheng());

        values.put("kaoqin",student.getKaoqin());

        Log.i("test",student.getKecheng());

        db.insert("kaoqin",null,values);

    }

    //插入学生信息

    public void insertstudent(int id,String password){

        SQLiteDatabase db=helper.getReadableDatabase();

        ContentValues values=new ContentValues();

        values.put("id",id);

        values.put("password",password);

        db.insert("denglu",null,values);

        Log.i("test","添加信息");

        db.close();

    }

    //删除学生信息

    public void delete(student student){

        SQLiteDatabase db=helper.getReadableDatabase();

        db.delete("denglu","id=?",new String[]{student.getId()+""});

        db.close();

    }

    //老师修改学生考勤情况

    public void update(student student){

        SQLiteDatabase db=helper.getReadableDatabase();

        ContentValues values=new ContentValues();

        values.put("id",student.getId());

        values.put("day",student.getDay());

        values.put("kecheng",student.getKecheng());

        values.put("kaoqin",student.getKaoqin());

        db.update("kaoqin",values,"id=?",new String[]{student.getId()+""});

        db.close();

    }

    //判断学号密码是否正确

    public boolean isboolean(int id,String password){

            SQLiteDatabase db=helper.getReadableDatabase();

            Cursor c=db.rawQuery("select * from denglu where password=? and id=?",new String[]{password,id+""});

            boolean b=c.moveToFirst();

            Log.i("test",b+"");

            return b;

    }

    //查看考勤情况

    public ArrayList< student > query(int id){

        ArrayList< student >list=new ArrayList< >();

        SQLiteDatabase db=helper.getReadableDatabase();

        Cursor c=db.rawQuery("select * from kaoqin where id=?",new String[]{id+""});

        while (c.moveToNext()){

            int i=c.getInt(0);

            String day=c.getString(1);

            String kecheng=c.getString(2);

            String kaoqin=c.getString(3);

            list.add(new student(i,day,kecheng,kaoqin));

        }

        db.close();

        return list;

    }

    //调用学生考勤日期

    public ArrayList< String >queryday(int id){

        ArrayList< String >day=new ArrayList< >();

        SQLiteDatabase db=helper.getReadableDatabase();

        Cursor c=db.rawQuery("select * from kaoqin where id=?",new String[]{id+""});

        while (c.moveToNext()){

            day.add(c.getString(1));

            Log.i("test",c.getString(1)+"");

        }

        return day;

    }

}

数据库:

public class help extends SQLiteOpenHelper {

    public help(Context context) {

        super(context, "shuju", null, 1);

    }

    @Override

    public void onCreate(SQLiteDatabase db) {

        db.execSQL("create table denglu(id integer,password)");

        db.execSQL("create table kaoqin(int id,day,kecheng,kaoqin)");

        Log.i("test","声明数据库");

    }

    @Override

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

    }

    @Override

    public void onOpen(SQLiteDatabase db) {

        super.onOpen(db);

    }

}

回复讨论(解决方案)

kaoqin表的id是string类型? 没有设置类型 没有设置类型是什么鬼,数据库中的字段难道没有类型吗? no such column id  没有id这个字段 个人觉得,应该是sql语句写的有点问题,导致数据库识别不到,请楼主仔细检查。安卓手写sqlit数据库太麻烦,不如用郭霖的litepal三方数据库,简单直接 找到问题了,我user表是后添加的,没有更新数据库,所以根本就没有user这个表。。。。

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新安卓软件开发学习
热门安卓软件开发学习
编程开发子分类